﻿Imports Crystal.CommonDBManagement.BLL.mdlCommon
Imports Crystal.CLL
Imports Crystal.CommonDBManagement.BLL
Imports System.Configuration
Imports System.IO
Imports System.Data
Imports System.Reflection
Imports Infragistics.Win.UltraWinGrid
Imports DevExpress.Utils
Imports System.Text

Public Class frmAddEditLKPTable
#Region "Declare"
    Dim msg As String = String.Empty
    Private cellValueBeforeEnter As String = String.Empty
    Dim combo As String
    Dim SelectedValue As String
    Dim TempDt As DataTable = Nothing
    Dim IsAddUpdateRecord As Boolean = False
    Dim _OnLoad As Boolean = False
    Dim isFromValidationForm As Boolean = False
    Dim selectedComboValue As String = String.Empty
    Dim newRecord As List(Of String)
    Dim CheckEventValidationForm As Boolean = False
    Dim CheckCancelEvent As Boolean = False
    Dim SelectedIndexChanged As Boolean = False
    Dim CheckAddRecord As Boolean = False
    Dim RecordSaved As Boolean = False
    Dim SelectedIndex As Integer = 0
    Dim autoWidth As Integer = 33
    Dim strPara As String
    Dim falseAfterValidationForm As Boolean = False
    Dim dataTable As DataTable = Nothing
    Dim updatedDataTable As DataTable = Nothing
    Dim ButtonAddNewEvent As Boolean = False
    Dim validationDataTable As DataTable
    Dim saveAndClose As Boolean = False
    Dim CheckSaved As Boolean = False
    Dim newRow As DataRow = Nothing
    Dim NotSavedData As DataTable = Nothing
#End Region
#Region "Properties"

    Public Property SelectValue() As String

        Get
            Return SelectedValue
        End Get

        Set(ByVal value As String)
            SelectedValue = value
        End Set

    End Property

    Public Property CheckValidationForm() As Boolean
        Get
            Return isFromValidationForm
        End Get

        Set(ByVal value As Boolean)
            isFromValidationForm = value
        End Set
    End Property

    Public Property NewRecords() As List(Of String)
        Get
            Return newRecord
        End Get

        Set(ByVal value As List(Of String))
            newRecord = value
        End Set
    End Property

    Public Property StrParam() As String
        Get
            Return strPara
        End Get

        Set(ByVal value As String)
            strPara = value
        End Set
    End Property

    Public Property ValidatedData() As DataTable
        Get
            Return validationDataTable
        End Get

        Set(ByVal value As DataTable)
            validationDataTable = value
        End Set
    End Property
#End Region
#Region "Form Events"

    Private Sub frmAddEditLKPTable_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        If saveAndClose = False Then
            If Not CheckGridExit() Then
                e.Cancel = True
            End If
        End If

    End Sub

    Private Sub frmAddEditLKPTable_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim dataGrid As New DataTable
        _OnLoad = True
        If CheckValidationForm Then
            CheckEventValidationForm = True
        End If

        InitControls()
        If ValidatedData IsNot Nothing Then
            If ValidatedData.Rows.Count > 0 Then
                BindComboInCell()
                If ValidatedData.Columns(0).ColumnName = "City" Then
                    dataGrid = CType(grdRecords.DataSource, DataTable)
                    For Each row As DataRow In ValidatedData.Rows
                        newRow = dataGrid.NewRow()
                        If row(0).ToString <> String.Empty Then
                            newRow("City") = row("City")
                            If row("CountryID").ToString = String.Empty Then
                                newRow("CountryID") = DBNull.Value
                            Else
                                newRow("CountryID") = row("CountryID")
                            End If
                        End If
                        dataGrid.Rows.Add(newRow)
                    Next
                    For Each row As UltraGridRow In grdRecords.Rows()
                        If row.Cells("CountryID").Value.ToString = DBNull.Value.ToString Then
                            row.Cells("CountryID").Appearance.BackColor = Color.Yellow
                        End If
                    Next
                ElseIf cmbMasterTable.Text.ToLower = "investor type linking" And ValidatedData.Columns(0).ColumnName = "Investor Type" Then
                    dataGrid = CType(grdRecords.DataSource, DataTable)
                    For Each row As DataRow In ValidatedData.Rows
                        newRow = dataGrid.NewRow()
                        If row(0).ToString <> String.Empty Then
                            newRow("InvestorTypeID") = row("Investor Type")
                            If row("Master Investor Type").ToString = String.Empty Then
                                newRow("MInvestorTypeID") = DBNull.Value 'row("MasterType") '
                            Else
                                newRow("MInvestorTypeID") = row("Master Investor Type")
                            End If
                        End If
                        dataGrid.Rows.Add(newRow)
                    Next
                    For Each row As UltraGridRow In grdRecords.Rows()
                        If row.Cells("MInvestorTypeID").Value.ToString = DBNull.Value.ToString Then
                            row.Cells("MInvestorTypeID").Appearance.BackColor = Color.Yellow
                        End If
                    Next

                Else
                    dataGrid = CType(grdRecords.DataSource, DataTable)
                    dataGrid.Merge(ValidatedData, True, MissingSchemaAction.Ignore)
                End If
            End If
        End If
        Dim band As UltraGridBand = Me.grdRecords.DisplayLayout.Bands(0)
        band.Columns(1).SortIndicator = SortIndicator.Ascending
        CheckEventValidationForm = False
        grdRecords.UpdateData()
        _OnLoad = False
    End Sub
#End Region
#Region "Form Load Methods"
    Private Sub InitControls()
        Me.Text = TitleManager.GetTitle("frmAddEditLKPTable")
        SetConnectionPath()
        InitCombos()
        LoadRefenceTables()
        If CheckValidationForm Then
            cmbMasterTable.SelectedIndex = SelectValue
            cmbMasterTable.Enabled = False
        Else
            cmbMasterTable.SelectedIndex = 1
        End If
        SelectedIndex = cmbMasterTable.SelectedIndex
    End Sub

#Region "Load Combobox Display Table Name"

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub InitCombos()
        cmbMasterTable.DisplayMember = "TableDisplayName"
        cmbMasterTable.ValueMember = "TableID"
    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub SetConnectionPath()
        BLLmdlCommon.strConfiguration = CommonCLL.mstrConnectionstring
    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub LoadRefenceTables()
        Dim eReturnVal As New ExecutionResult
        Dim objReferenceTable As New cAddEditLKPTable
        Dim dtSet As New DataSet
        Try
            eReturnVal = objReferenceTable.LoadReferenceTable()
            dtSet = CType(eReturnVal.ResultObject, DataSet)
            If dtSet IsNot Nothing Then
                If dtSet.Tables.Count > 0 Then
                    If CommonDBManagement.BLL.AddRowInDropDownAtFirst(dtSet.Tables(0), cmbMasterTable.ValueMember, cmbMasterTable.DisplayMember, m_strSelectIDValue, m_strSelectTextValue) = False Then
                        ShowMessageBox(DBResult.ERROR_DISPLAYTABLE_COMBO)
                    End If
                    cmbMasterTable.DataSource = dtSet.Tables(0)
                End If
            End If
        Catch ex As Exception
            If (Not Me.IsDisposed) Then
                MessageBox.Show(ex.Message)
            End If
        Finally
            eReturnVal = Nothing
            objReferenceTable = Nothing
            dtSet = Nothing
        End Try
    End Sub
#End Region
#End Region

#Region "Combo Master Table Events"
    Private Sub cmbMasterTable_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmbMasterTable.SelectedIndexChanged

        dataTable = Nothing
        updatedDataTable = Nothing
        If cmbMasterTable.DataSource IsNot Nothing Then
            If cmbMasterTable.SelectedIndex <> 0 Then
                If _OnLoad = False Then
                    If checkForChangeInCell() = True Then
                        combo = cmbMasterTable.Text
                        BindComboInCell()
                        BindGridData()
                    End If
                Else
                    combo = cmbMasterTable.Text
                    BindComboInCell()
                    BindGridData()
                End If
            Else
                If cmbMasterTable.SelectedIndex = 0 Then
                    If Not _OnLoad Then
                        MessageInfo("Invalid table selection.")
                        cmbMasterTable.SelectedIndex = 1
                    End If
                End If
            End If
        End If
    End Sub
#End Region

#Region "Bind Grid Data"

    Private Sub BindGridData()
        Dim eReturnVal As New ExecutionResult
        Dim objReferenceTable As New cAddEditLKPTable
        Dim dtSet As New DataSet
        Dim table As New DataTable
        Dim waitDialog As New WaitDialogForm()
        pnlButton.Enabled = False
        Try

            If CheckValidationForm And strPara <> Nothing Then
                dtSet = objReferenceTable.GetCities(strPara)
            Else
                eReturnVal = objReferenceTable.LoadReferenceTableData(cmbMasterTable.SelectedValue)
                dtSet = CType(eReturnVal.ResultObject, DataSet)
            End If
            If dtSet IsNot Nothing Then
                If dtSet.Tables.Count > 0 Then
                    If Not dtSet.Tables(0).Columns.Contains("Delete") Then
                        dtSet.Tables(0).Columns.Add("Delete", GetType(Bitmap))
                    End If
                    table = dtSet.Tables(0)
                    grdRecords.DataSource = table
                    If grdRecords.Rows.Band.Columns.Exists("MInvestorTypeID") And cmbMasterTable.Text.ToLower() = "investor type linking" Then
                        grdRecords.Rows.Band.Columns("MInvestorTypeID").Hidden = False
                    End If
                    grdRecords.UpdateData()
                    TempDt = dtSet.Tables(0).Clone()
                    waitDialog.Close()
                    pnlButton.Enabled = True
                    lblTotalRecords.Text = "Total records :- " + grdRecords.Rows.Count.ToString()
                End If
            End If
        Catch ex As Exception
            If (Not Me.IsDisposed) Then
                MessageBox.Show(ex.Message)
            End If
        Finally
            eReturnVal = Nothing
            waitDialog.Close()
            objReferenceTable = Nothing
            dtSet = Nothing
        End Try
    End Sub

    Private Sub AddNewRecords()
        Select Case SelectValue.ToLower.ToString.Trim
            Case LKPRefTable.City
                cmbMasterTable.SelectedIndex = LKPRefTable.City
                Dim i As Integer
                For i = 0 To NewRecords.Count - 1
                    grdRecords.DisplayLayout.Bands(0).AddNew()
                    grdRecords.Rows(grdRecords.Rows.Count - 1).Cells("City").Value = NewRecords.Item(i)
                Next

            Case LKPRefTable.Country
                cmbMasterTable.SelectedIndex = LKPRefTable.Country
                Dim i As Integer
                For i = 0 To NewRecords.Count - 1
                    grdRecords.DisplayLayout.Bands(0).AddNew()
                    grdRecords.Rows(grdRecords.Rows.Count - 1).Cells("CountryCode").Value = NewRecords.Item(i)
                Next

            Case LKPRefTable.InvestorType
                cmbMasterTable.SelectedIndex = LKPRefTable.InvestorType
                Dim i As Integer
                For i = 0 To NewRecords.Count - 1
                    grdRecords.DisplayLayout.Bands(0).AddNew()
                    grdRecords.Rows(grdRecords.Rows.Count - 1).Cells("InvestorType").Value = NewRecords.Item(i)
                    grdRecords.Rows(grdRecords.Rows.Count - 1).Cells("IsExcludedFromSearch").Value = False
                Next

            Case LKPRefTable.State
                cmbMasterTable.SelectedIndex = LKPRefTable.State
                Dim i As Integer
                For i = 0 To NewRecords.Count - 1
                    grdRecords.DisplayLayout.Bands(0).AddNew()
                    grdRecords.Rows(grdRecords.Rows.Count - 1).Cells("StateCode").Value = NewRecords.Item(i)
                Next

        End Select


    End Sub

#End Region

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function DonotUpdateIfChangesNotMade() As Boolean
        Dim objmaster As cAddEditLKPTable = Nothing
        Dim existence As Integer = 0
        Dim ID As Integer = 0
        Dim value As String = grdRecords.ActiveRow.Cells(0).Text
        If value <> String.Empty Then
            ID = grdRecords.ActiveRow.Cells(0).Value
        End If
        objmaster = New cAddEditLKPTable
        Select Case combo.Trim().ToLower

            Case "activist"
                existence = objmaster.Donotupdateifchangesnotmade(combo.Trim.ToLower, ID, _
                                                           grdRecords.ActiveRow.Cells("ActivistCategory").Text.Trim(), grdRecords.ActiveRow.Cells("ActivistCode").Text.Trim())

            Case "city"
                existence = objmaster.Donotupdateifchangesnotmade(combo.Trim.ToLower, ID, _
                                                           grdRecords.ActiveRow.Cells("city").Text.Trim(), grdRecords.ActiveRow.Cells("CountryID").Value.ToString)

            Case "country"
                existence = objmaster.Donotupdateifchangesnotmade(combo.Trim.ToLower, ID, _
                                                           grdRecords.ActiveRow.Cells("CountryCode").Text.Trim(), grdRecords.ActiveRow.Cells("CountryName").Text, _
                                                           grdRecords.ActiveRow.Cells("RegionID").Value.ToString)

            Case "exchange"
                existence = objmaster.Donotupdateifchangesnotmade(combo.Trim.ToLower, ID, _
                                                           grdRecords.ActiveRow.Cells("exchange").Text.Trim())

            Case "fund type"
                existence = objmaster.Donotupdateifchangesnotmade(combo.Trim.ToLower, ID, _
                                                           grdRecords.ActiveRow.Cells("FundType").Text.Trim(), grdRecords.ActiveRow.Cells("FundTypeDescription").Value)

            Case "head investor"
                existence = objmaster.Donotupdateifchangesnotmade(combo.Trim.ToLower, ID, _
                                                           grdRecords.ActiveRow.Cells("HeadInvestor").Text.Trim())

            Case "industry"
                existence = objmaster.Donotupdateifchangesnotmade(combo.Trim.ToLower, ID, _
                                                           grdRecords.ActiveRow.Cells("Industry").Text.Trim())

            Case "investor type"
                If grdRecords.ActiveRow.Cells("InvestorTypeID").Value.ToString = String.Empty Then
                    Return False
                End If
                If grdRecords.ActiveRow.Cells("IsExcludedFromSearch").Value.ToString = String.Empty Then
                    grdRecords.ActiveRow.Cells("IsExcludedFromSearch").Value = False
                End If
                existence = objmaster.Donotupdateifchangesnotmade(combo.Trim.ToLower, ID, _
                                                           grdRecords.ActiveRow.Cells("InvestorType").Text.Trim(), grdRecords.ActiveRow.Cells("InvestorTypeCode").Text.Trim, _
                                                           grdRecords.ActiveRow.Cells("IsExcludedFromSearch").Value)

            Case "metro"
                existence = objmaster.Donotupdateifchangesnotmade(combo.Trim.ToLower, ID, _
                                                           grdRecords.ActiveRow.Cells("Metro").Text.Trim())

            Case "region"
                existence = objmaster.Donotupdateifchangesnotmade(combo.Trim.ToLower, ID, grdRecords.ActiveRow.Cells("RegionCode").Text.Trim(), _
                                                           grdRecords.ActiveRow.Cells("Region").Text.Trim())

            Case "sector"
                existence = objmaster.Donotupdateifchangesnotmade(combo.Trim.ToLower, ID, _
                                                           grdRecords.ActiveRow.Cells("Sector").Text.Trim())

            Case "state"
                existence = objmaster.Donotupdateifchangesnotmade(combo.Trim.ToLower, ID, grdRecords.ActiveRow.Cells("StateCode").Text.Trim(), _
                                                           grdRecords.ActiveRow.Cells("State").Text.Trim(), grdRecords.ActiveRow.Cells("CountryID").Value)

            Case "style"
                existence = objmaster.Donotupdateifchangesnotmade(combo.Trim.ToLower, ID, _
                                                           grdRecords.ActiveRow.Cells("Style").Text.Trim())

            Case "territory"
                existence = objmaster.Donotupdateifchangesnotmade(combo.Trim.ToLower, ID, _
                                                           grdRecords.ActiveRow.Cells("Territory").Text.Trim())

            Case "ultimate parent"
                existence = objmaster.Donotupdateifchangesnotmade(combo.Trim.ToLower, ID, _
                                                           grdRecords.ActiveRow.Cells("UltimateParent").Text.Trim())

            Case "master investor type"
                existence = objmaster.Donotupdateifchangesnotmade(combo.Trim.ToLower, ID, _
                                                           grdRecords.ActiveRow.Cells("MInvestorType").Text.Trim(), grdRecords.ActiveRow.Cells("MInvestorTypeCode").Text.Trim())

            Case "turnover"
                existence = objmaster.Donotupdateifchangesnotmade(combo.Trim.ToLower, ID, _
                                                           grdRecords.ActiveRow.Cells("Turnover").Text.Trim())

            Case "investor type linking"
                existence = objmaster.Donotupdateifchangesnotmade(combo.Trim.ToLower, ID, _
                                                           grdRecords.ActiveRow.Cells("InvestorTypeID").Value.ToString, grdRecords.ActiveRow.Cells("MInvestorTypeID").Value.ToString)
        End Select
        Return existence
    End Function

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function RecordAlreadyExist() As String
        Dim objmaster As cAddEditLKPTable = Nothing
        Dim dataExist As String = String.Empty
        Dim ID As Integer = 0
        Dim value As String = grdRecords.ActiveRow.Cells(0).Text
        If value <> String.Empty Then
            ID = grdRecords.ActiveRow.Cells(0).Value
        End If
        objmaster = New cAddEditLKPTable
        Select Case combo.Trim().ToLower

            Case "activist"
                dataExist = objmaster.CheckRecordDataExist(combo.Trim.ToLower, ID, _
                                                           grdRecords.ActiveRow.Cells("ActivistCategory").Text.Trim(), grdRecords.ActiveRow.Cells("ActivistCode").Text.Trim())

            Case "city"
                dataExist = objmaster.CheckRecordDataExist(combo.Trim.ToLower, ID, _
                                                           grdRecords.ActiveRow.Cells("city").Text.Trim(), grdRecords.ActiveRow.Cells("CountryID").Value)

            Case "country"
                dataExist = objmaster.CheckRecordDataExist(combo.Trim.ToLower, ID, _
                                                           grdRecords.ActiveRow.Cells("CountryCode").Text.Trim(), grdRecords.ActiveRow.Cells("CountryName").Value, _
                                                           grdRecords.ActiveRow.Cells("RegionID").Value)

            Case "exchange"
                dataExist = objmaster.CheckRecordDataExist(combo.Trim.ToLower, ID, _
                                                           grdRecords.ActiveRow.Cells("exchange").Text.Trim())

            Case "fund type"
                dataExist = objmaster.CheckRecordDataExist(combo.Trim.ToLower, ID, _
                                                           grdRecords.ActiveRow.Cells("FundType").Text.Trim(), grdRecords.ActiveRow.Cells("FundTypeDescription").Value)

            Case "head investor"
                dataExist = objmaster.CheckRecordDataExist(combo.Trim.ToLower, ID, _
                                                           grdRecords.ActiveRow.Cells("HeadInvestor").Text.Trim())

            Case "industry"
                dataExist = objmaster.CheckRecordDataExist(combo.Trim.ToLower, ID, _
                                                           grdRecords.ActiveRow.Cells("Industry").Text.Trim())

            Case "investor type"
                dataExist = objmaster.CheckRecordDataExist(combo.Trim.ToLower, ID, _
                                                           grdRecords.ActiveRow.Cells("InvestorType").Text.Trim(), grdRecords.ActiveRow.Cells("IsExcludedFromSearch").Value)

            Case "metro"
                dataExist = objmaster.CheckRecordDataExist(combo.Trim.ToLower, ID, _
                                                           grdRecords.ActiveRow.Cells("Metro").Text.Trim())

            Case "region"
                dataExist = objmaster.CheckRecordDataExist(combo.Trim.ToLower, ID, grdRecords.ActiveRow.Cells("RegionCode").Text.Trim(), _
                                                           grdRecords.ActiveRow.Cells("Region").Text.Trim())

            Case "sector"
                dataExist = objmaster.CheckRecordDataExist(combo.Trim.ToLower, ID, _
                                                           grdRecords.ActiveRow.Cells("Sector").Text.Trim())

            Case "state"
                dataExist = objmaster.CheckRecordDataExist(combo.Trim.ToLower, ID, grdRecords.ActiveRow.Cells("StateCode").Text.Trim(), _
                                                           grdRecords.ActiveRow.Cells("State").Text.Trim(), grdRecords.ActiveRow.Cells("CountryID").Value)

            Case "style"
                dataExist = objmaster.CheckRecordDataExist(combo.Trim.ToLower, ID, _
                                                           grdRecords.ActiveRow.Cells("Style").Text.Trim())

            Case "territory"
                dataExist = objmaster.CheckRecordDataExist(combo.Trim.ToLower, ID, _
                                                           grdRecords.ActiveRow.Cells("Territory").Text.Trim())

            Case "ultimate parent"
                dataExist = objmaster.CheckRecordDataExist(combo.Trim.ToLower, ID, _
                                                           grdRecords.ActiveRow.Cells("UltimateParent").Text)
        End Select
        Return dataExist
    End Function

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function CheckGridExit() As Boolean
        If grdRecords Is Nothing Then Return True
        If grdRecords.ActiveRow Is Nothing Then Return True
        If grdRecords.ActiveRow.Cells(0).Value.ToString = String.Empty Then
            If cmbMasterTable.Text = "Link Investor Type" Then
                If grdRecords.ActiveRow.Cells("MInvestorTypeID").Appearance.BackColor = Color.Yellow Then
                    Return True
                End If
            End If
            '.SelectedItem Then
            Validation(msg)
            If msg <> String.Empty Then
                If grdRecords.ActiveRow IsNot Nothing Then
                    If grdRecords.ActiveRow.Appearance.BackColor = Color.Red Then
                        If (MessageBox.Show("Do you want to exit without make any change?", m_strMsgBoxCaption, MessageBoxButtons.YesNo, MessageBoxIcon.Information) = Windows.Forms.DialogResult.Yes) Then
                            Return True
                        Else
                            Return False
                        End If
                    End If
                End If
                MessageBox.Show(msg, BaseClass.crystalCaption, MessageBoxButtons.OK, MessageBoxIcon.Information)
                Return False
            End If
        End If
        If (DonotUpdateIfChangesNotMade() = 0) Then
            If (MessageBox.Show("Do you want to save the changes?", m_strMsgBoxCaption, MessageBoxButtons.YesNo, MessageBoxIcon.Information) = Windows.Forms.DialogResult.Yes) Then
                btnSave.PerformClick()
                If CheckSaved = True Then
                    Return True
                Else
                    Return False
                End If
            Else
                Return True
            End If
        End If
        Return True
    End Function

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function checkForChangeInCell() As Boolean
        Dim dataExists As String = String.Empty
        If grdRecords Is Nothing Then Return True
        If grdRecords.ActiveRow Is Nothing Then Return True
        If Validation(msg) Then
            If (DonotUpdateIfChangesNotMade() = 0) Then
                If (MessageBox.Show("Do you want to save the changes?", m_strMsgBoxCaption, MessageBoxButtons.YesNo, MessageBoxIcon.Information) = Windows.Forms.DialogResult.Yes) Then
                    dataExists = RecordAlreadyExist()
                    If dataExists = String.Empty Then
                        btnSave.PerformClick()
                        lblTotalRecords.Text = "Total records :- " + grdRecords.Rows.Count.ToString()
                        Return True
                    Else
                        ShowMessageBox(DBResult.RECORD_ALREADY_EXIST, dataExists)
                        RecordSaved = False
                        Return False
                    End If
                End If
            End If
        Else
            If msg <> String.Empty Then
                MessageBox.Show(msg, BaseClass.crystalCaption, MessageBoxButtons.OK, MessageBoxIcon.Information)
                Return False
            End If
        End If
        Return True
    End Function

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub GetUpdatedRows()
        Dim row As UltraGridRow
        Dim dtRecords As New DataTable
        Dim drv As DataRowView
        Dim drow As DataRow
        Try
            If updatedDataTable Is Nothing Then
                dtRecords = CType(grdRecords.DataSource, DataTable)
                updatedDataTable = dtRecords.Clone()
            End If
            row = CType(grdRecords.ActiveRow, UltraGridRow)
            drv = TryCast(row.ListObject, DataRowView)
            drow = updatedDataTable.NewRow()
            drow = If(drv IsNot Nothing, drv.Row, Nothing)
            updatedDataTable.ImportRow(drow)
        Catch ex As Exception
            MessageBox.Show(ex.Message, BaseClass.crystalCaption, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            row = Nothing
            drv = Nothing
            drow = Nothing
            dtRecords = Nothing
        End Try
    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub grdRecords_BeforeRowDeactivate(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles grdRecords.BeforeRowDeactivate
        Dim objReferenceTableInfo As New cAddEditLKPTable
        Dim dataExists As String = String.Empty
        Try
            If (grdRecords.ActiveRow.Cells(0).Text <> "") Then
                If Validation(msg) Then
                    If (DonotUpdateIfChangesNotMade() = 0) Then
                        dataExists = RecordAlreadyExist()
                        If dataExists = String.Empty Then
                            GetUpdatedRows()
                        Else
                            ShowMessageBox(DBResult.RECORD_ALREADY_EXIST, dataExists)
                            For Each cell As UltraGridCell In grdRecords.ActiveRow.Cells
                                If cell.Value.ToString = dataExists Then
                                    '    cell.Value = String.Empty
                                    cell.Activate()
                                End If
                            Next
                            e.Cancel = True
                        End If
                    End If
                Else
                    MessageBox.Show(msg, BaseClass.crystalCaption, MessageBoxButtons.OK, MessageBoxIcon.Information)
                    e.Cancel = True
                End If
            Else
                If Validation(msg) Then
                    'MessageBox.Show(msg, BaseClass.crystalCaption, MessageBoxButtons.OK, MessageBoxIcon.Information)
                    'e.Cancel = True
                    'Else
                    '    dataExists = RecordAlreadyExist()
                    '    ShowMessageBox(DBResult.RECORD_ALREADY_EXIST, dataExists)
                    '    For Each cell As UltraGridCell In grdRecords.ActiveRow.Cells
                    '        If cell.Value.ToString = dataExists Then
                    '            cell.Value = String.Empty
                    '            cell.Activate()
                    '        End If
                    '    Next
                    '    e.Cancel = True
                End If
                If msg <> String.Empty Then
                    MessageBox.Show(msg, BaseClass.crystalCaption, MessageBoxButtons.OK, MessageBoxIcon.Information)
                    e.Cancel = True
                    Exit Sub
                End If
            End If
            If updatedDataTable IsNot Nothing Then
                If updatedDataTable.Rows.Count > 0 Then
                    'btnSave.PerformClick()
                End If
            End If
            grdRecords.UpdateData()

        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    ''' <summary>
    ''' saves the data in database
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Sub CheckSameRecord(ByRef table As DataTable)
        Dim cloneTable As New DataTable
        cloneTable = table.Clone()
        Dim matched As Boolean = False
        If table.Rows.Count > 0 Then
            For i As Integer = 0 To table.Rows.Count - 1
                If i < table.Rows.Count - 1 Then
                    Dim comparer As IEqualityComparer(Of DataRow) = DataRowComparer.Default
                    Dim bEqual = comparer.Equals(table.Rows(i), table.Rows(i + 1))
                    If bEqual Then
                        cloneTable.ImportRow(table.Rows(i))
                    End If
                End If
            Next

        End If
        If cloneTable.Rows.Count > 0 Then
            Dim indexRow As Integer
            For Each row As DataRow In cloneTable.Rows
                indexRow = FindRowIndex(table, table.Columns(0).ColumnName.ToString, row(0).ToString())
                table.Rows(indexRow).Delete()
                table.AcceptChanges()
            Next
        End If
        table.AcceptChanges()

    End Sub


    Private Sub CheckAddUpdateRecord(ByRef table As DataTable, ByVal isTrue As Boolean)
        Dim message As String = ""
        Dim isNew As Boolean = False
        Dim recordID As Integer = 0
        Dim recordExists As New DataTable
        recordExists = table.Clone()
        Dim objReferenceTableInfo As New cAddEditLKPTable
        Select Case combo.ToLower().Trim().ToString()
            Case "activist"
                If isTrue Then
                    For Each row As DataRow In table.Rows
                        If objReferenceTableInfo.AddUpdateRecord(combo.ToLower.Trim, "", row("ActivistCategory").ToString, row("ActivistCode").ToString) = False Then
                            recordExists.ImportRow(row)
                            'table.Rows.Remove(row)
                            'table.AcceptChanges()
                        End If
                    Next
                Else
                    For Each row As DataRow In table.Rows
                        If objReferenceTableInfo.AddUpdateRecord(combo.ToLower.Trim, row("ActivistID").ToString, row("ActivistCategory").ToString, row("ActivistCode").ToString) = False Then
                            recordExists.ImportRow(row)
                        End If
                    Next
                End If
            Case "city"
                If isTrue Then
                    For Each row As DataRow In table.Rows
                        If (objReferenceTableInfo.AddUpdateRecord(combo.ToLower.Trim, "", row("City").ToString, _
                                                              row("CountryID").ToString)) = False Then
                            recordExists.ImportRow(row)
                        End If
                    Next
                Else
                    For Each row As DataRow In table.Rows
                        If (objReferenceTableInfo.AddUpdateRecord(combo.ToLower.Trim, row(0).ToString, row("City").ToString, _
                                                              row("CountryID").ToString)) = False Then
                            recordExists.ImportRow(row)
                        End If
                    Next
                End If
            Case "country"
                If isTrue Then
                    For Each row As DataRow In table.Rows
                        If (objReferenceTableInfo.AddUpdateRecord(combo.ToLower.Trim, "", row("CountryCode").ToString, _
                                                             row("CountryName").ToString, row("RegionID").ToString)) = False Then
                            recordExists.ImportRow(row)
                        End If
                    Next
                Else
                    For Each row As DataRow In table.Rows
                        If (objReferenceTableInfo.AddUpdateRecord(combo.ToLower.Trim, row(0).ToString, row("CountryCode").ToString, _
                                                             row("CountryName").ToString, row("RegionID").ToString)) = False Then
                            recordExists.ImportRow(row)
                        End If
                    Next
                End If

            Case "exchange"
                If isTrue Then
                    For Each row As DataRow In table.Rows
                        If (objReferenceTableInfo.AddUpdateRecord(combo.ToLower.Trim, "", row("Exchange").ToString)) = False Then
                            recordExists.ImportRow(row)
                        End If
                    Next
                Else
                    For Each row As DataRow In table.Rows
                        If (objReferenceTableInfo.AddUpdateRecord(combo.ToLower.Trim, row(0).ToString, row("Exchange").ToString)) = False Then
                            recordExists.ImportRow(row)
                        End If
                    Next
                End If


            Case "fund type"
                If isTrue Then
                    For Each row As DataRow In table.Rows
                        If (objReferenceTableInfo.AddUpdateRecord(combo.ToLower.Trim, "", row("FundType").ToString, _
                                                              row("FundTypeDescription").ToString)) = False Then
                            recordExists.ImportRow(row)
                        End If
                    Next
                Else
                    For Each row As DataRow In table.Rows
                        If (objReferenceTableInfo.AddUpdateRecord(combo.ToLower.Trim, row(0).ToString, row("FundType").ToString, _
                                                              row("FundTypeDescription").ToString)) = False Then
                            recordExists.ImportRow(row)
                        End If
                    Next
                End If


            Case "head investor"
                If isTrue Then
                    For Each row As DataRow In table.Rows
                        If (objReferenceTableInfo.AddUpdateRecord(combo.ToLower.Trim, "", row("HeadInvestor").ToString)) = False Then
                            recordExists.ImportRow(row)
                        End If
                    Next
                Else
                    For Each row As DataRow In table.Rows
                        If (objReferenceTableInfo.AddUpdateRecord(combo.ToLower.Trim, row(0).ToString, row("HeadInvestor").ToString)) = False Then
                            recordExists.ImportRow(row)

                        End If
                    Next
                End If


            Case "industry"
                If isTrue Then
                    For Each row As DataRow In table.Rows
                        If (objReferenceTableInfo.AddUpdateRecord(combo.ToLower.Trim, "", row("Industry").ToString)) = False Then
                            recordExists.ImportRow(row)

                        End If
                    Next
                Else
                    For Each row As DataRow In table.Rows
                        If (objReferenceTableInfo.AddUpdateRecord(combo.ToLower.Trim, row(0).ToString, row("Industry").ToString)) = False Then
                            recordExists.ImportRow(row)

                        End If
                    Next
                End If


            Case "investor type"
                If isTrue Then
                    For Each row As DataRow In table.Rows
                        If (objReferenceTableInfo.AddUpdateRecord(combo.ToLower.Trim, "", row("InvestorType").ToString _
                                                              , row("IsExcludedFromSearch").ToString)) = False Then
                            recordExists.ImportRow(row)

                        End If
                    Next
                Else
                    For Each row As DataRow In table.Rows
                        If (objReferenceTableInfo.AddUpdateRecord(combo.ToLower.Trim, row(0).ToString, row("InvestorType").ToString _
                                                              , row("IsExcludedFromSearch").ToString)) = False Then
                            recordExists.ImportRow(row)

                        End If
                    Next
                End If


            Case "metro"
                If isTrue Then
                    For Each row As DataRow In table.Rows
                        If (objReferenceTableInfo.AddUpdateRecord(combo.ToLower.Trim, String.Empty, row("Metro").ToString)) = False Then

                            recordExists.ImportRow(row)
                        End If
                    Next
                Else
                    For Each row As DataRow In table.Rows
                        If (objReferenceTableInfo.AddUpdateRecord(combo.ToLower.Trim, row(0).ToString, row("Metro").ToString)) = False Then

                            recordExists.ImportRow(row)
                        End If
                    Next
                End If



            Case "region"
                If isTrue Then
                    For Each row As DataRow In table.Rows
                        If (objReferenceTableInfo.AddUpdateRecord(combo.ToLower.Trim, String.Empty, row("Region").ToString, _
                                                             row("RegionCode").ToString)) = False Then
                            recordExists.ImportRow(row)

                        End If
                    Next
                Else
                    For Each row As DataRow In table.Rows
                        If (objReferenceTableInfo.AddUpdateRecord(combo.ToLower.Trim, row(0).ToString, row("Region").ToString, _
                                                             row("RegionCode").ToString)) = False Then
                            recordExists.ImportRow(row)

                        End If
                    Next
                End If



            Case "sector"
                If isTrue Then
                    For Each row As DataRow In table.Rows
                        If (objReferenceTableInfo.AddUpdateRecord(combo.ToLower.Trim, String.Empty, row("Sector").ToString)) = False Then
                            recordExists.ImportRow(row)

                        End If
                    Next
                Else
                    For Each row As DataRow In table.Rows
                        If (objReferenceTableInfo.AddUpdateRecord(combo.ToLower.Trim, row(0).ToString, row("Sector").ToString)) = False Then
                            recordExists.ImportRow(row)

                        End If
                    Next
                End If


            Case "state"
                If isTrue Then
                    For Each row As DataRow In table.Rows
                        If (objReferenceTableInfo.AddUpdateRecord(combo.ToLower.Trim, String.Empty, row("State").ToString, _
                                                             row("StateCode").ToString, row("CountryID").ToString)) = False Then

                            recordExists.ImportRow(row)
                        End If
                    Next
                Else
                    For Each row As DataRow In table.Rows
                        If (objReferenceTableInfo.AddUpdateRecord(combo.ToLower.Trim, row(0).ToString, row("State").ToString, _
                                                             row("StateCode").ToString, row("CountryID").ToString)) = False Then

                            recordExists.ImportRow(row)
                        End If
                    Next
                End If



            Case "style"
                If isTrue Then
                    For Each row As DataRow In table.Rows
                        If (objReferenceTableInfo.AddUpdateRecord(combo.ToLower.Trim, String.Empty, row("Style").ToString)) = False Then
                            recordExists.ImportRow(row)

                        End If
                    Next
                Else
                    For Each row As DataRow In table.Rows
                        If (objReferenceTableInfo.AddUpdateRecord(combo.ToLower.Trim, row(0).ToString, row("Style").ToString)) = False Then
                            recordExists.ImportRow(row)

                        End If
                    Next
                End If


            Case "territory"
                If isTrue Then
                    For Each row As DataRow In table.Rows
                        If (objReferenceTableInfo.AddUpdateRecord(combo.ToLower.Trim, String.Empty, row("Territory").ToString)) = False Then
                            recordExists.ImportRow(row)

                        End If
                    Next
                Else
                    For Each row As DataRow In table.Rows
                        If (objReferenceTableInfo.AddUpdateRecord(combo.ToLower.Trim, row(0).ToString, row("Territory").ToString)) = False Then
                            recordExists.ImportRow(row)

                        End If
                    Next
                End If


            Case "ultimate parent"
                If isTrue Then
                    For Each row As DataRow In table.Rows
                        If (objReferenceTableInfo.AddUpdateRecord(combo.ToLower.Trim, String.Empty, row("UltimateParent").ToString)) = False Then
                            recordExists.ImportRow(row)

                        End If
                    Next
                Else
                    For Each row As DataRow In table.Rows
                        If (objReferenceTableInfo.AddUpdateRecord(combo.ToLower.Trim, row("UltimateParentID").ToString, row("UltimateParent").ToString)) = False Then
                            recordExists.ImportRow(row)

                        End If
                    Next
                End If

            Case "turnover"
                If isTrue Then
                    For Each row As DataRow In table.Rows
                        If (objReferenceTableInfo.AddUpdateRecord(combo.ToLower.Trim, String.Empty, row("Turnover").ToString)) = False Then
                            recordExists.ImportRow(row)
                        End If
                    Next
                Else
                    For Each row As DataRow In table.Rows
                        If (objReferenceTableInfo.AddUpdateRecord(combo.ToLower.Trim, row("TurnoverID").ToString, row("Turnover").ToString)) = False Then
                            recordExists.ImportRow(row)
                        End If
                    Next
                End If


            Case "investor type linking"
                If isTrue Then
                    For Each row As DataRow In table.Rows
                        If (objReferenceTableInfo.AddUpdateRecord(combo.ToLower.Trim, String.Empty, row("InvestorTypeID").ToString, _
                                                                  row("MInvestorTypeID").ToString)) = False Then
                            recordExists.ImportRow(row)
                        End If
                    Next
                Else
                    For Each row As DataRow In table.Rows
                        If (objReferenceTableInfo.AddUpdateRecord(combo.ToLower.Trim, row("TypeMTypeLinkID").ToString, row("InvestorTypeID").ToString, _
                                                                  row("MInvestorTypeID").ToString)) = False Then
                            recordExists.ImportRow(row)
                        End If
                    Next
                End If

        End Select
        If recordExists.Rows.Count > 0 Then
            Dim indexRow As Integer
            For Each row As DataRow In recordExists.Rows
                indexRow = FindRowIndex(table, table.Columns(0).ColumnName.ToString, row(0).ToString())
                table.Rows(indexRow).Delete()
                table.AcceptChanges()
            Next
        End If
        dataTable.AcceptChanges()
    End Sub

    Private Sub grdRecords_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles grdRecords.Click
        Dim dtSet As New DataTable
        Dim objReferenceTableInfo As New cAddEditLKPTable
        Dim eReturnVal As New ExecutionResult
        Dim dataExists As String = String.Empty
        Dim index As Integer = 0
        Dim indexRow As Integer = -1
        Dim dtRecords As New DataTable
        Dim deletedRowTable As New DataTable
        ' Dim deletedRow As DataRow
        Try
            If grdRecords.ActiveCell Is Nothing Then
                Exit Sub
            End If
            If grdRecords.ActiveRow IsNot Nothing Then
                index = grdRecords.ActiveRow.Index
                index = index - 1
            End If
            If grdRecords.ActiveCell.Column.Header.Caption = "Delete" Then
                If (grdRecords.ActiveRow.Cells(0).Text = "") Then
                    grdRecords.ActiveRow.Delete(False)
                    If index >= 0 And index <= grdRecords.Rows.Count - 1 Then
                        If grdRecords.ActiveRow Is Nothing Then
                            grdRecords.Rows(index).Activate()
                        End If
                    End If
                    'grdRecords.ActiveCell = Nothing
                    Exit Sub
                End If
                If (MessageBox.Show("Do you want to delete?", mstrMsgBoxCaption, MessageBoxButtons.OKCancel, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Cancel) Then
                    grdRecords.ActiveCell = Nothing
                    Return
                End If
                If (CheckBeforeDeletion() = False) Then
                    Dim vale As String = grdRecords.ActiveRow.Cells(1).Value.ToString
                    deletedRowTable = CType(grdRecords.DataSource, DataTable).Clone()
                    'deletedRow = CType(grdRecords.ActiveRow, DataRow)
                    Dim gridRow As UltraGridRow = grdRecords.ActiveRow
                    Dim drv As DataRowView = TryCast(gridRow.ListObject, DataRowView)
                    Dim row As DataRow = If(drv IsNot Nothing, drv.Row, Nothing)
                    deletedRowTable.ImportRow(row)
                    RaiseEventForDelete(deletedRowTable, True)
                    ShowMessageBox(DBResult.RECORDS_DELETED)
                    Exit Sub
                End If
                If (objReferenceTableInfo.CheckBeforeDelete(cmbMasterTable.SelectedValue, grdRecords.ActiveRow.Cells(0).Value) = True) Then
                    If objReferenceTableInfo.DeleteReferenceTableData(cmbMasterTable.SelectedValue, grdRecords.ActiveRow.Cells(0).Value) = True Then
                        Dim vale As String = grdRecords.ActiveRow.Cells(1).Value.ToString
                        deletedRowTable = CType(grdRecords.DataSource, DataTable).Clone()
                        'deletedRow = CType(grdRecords.ActiveRow, DataRow)
                        Dim gridRow As UltraGridRow = grdRecords.ActiveRow
                        Dim drv As DataRowView = TryCast(gridRow.ListObject, DataRowView)
                        Dim row As DataRow = If(drv IsNot Nothing, drv.Row, Nothing)
                        deletedRowTable.ImportRow(row)
                        RaiseEventForDelete(deletedRowTable, True)
                        ShowMessageBox(DBResult.RECORDS_DELETED)
                        lblTotalRecords.Text = "Total records :- " + grdRecords.Rows.Count.ToString()

                    End If
                Else
                    ShowMessageBox(DBResult.RECORD_CNT_DELETE)
                End If

            End If

        Catch ex As Exception
            If (Not Me.IsDisposed) Then
                MessageBox.Show(ex.Message)
            End If
        Finally
            objReferenceTableInfo = Nothing
            dtSet = Nothing
            eReturnVal = Nothing
        End Try
    End Sub

    Private Sub grdRecords_InitializeLayout(ByVal sender As Object, ByVal e As Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs) Handles grdRecords.InitializeLayout
        e.Layout.Bands(0).Columns("Delete").CellAppearance.ImageHAlign = Infragistics.Win.HAlign.Center
        e.Layout.Bands(0).Columns(0).Hidden = True
        e.Layout.AutoFitStyle = AutoFitStyle.None
        If e.Layout.Bands(0).Columns.Exists("IsDefault") Then
            e.Layout.Bands(0).Columns("IsDefault").Hidden = True
        End If
        Select Case combo.ToLower().Trim().ToString()
            Case "activist"

            Case "city"
                e.Layout.Bands(0).Columns("CountryID").Hidden = False
                e.Layout.Bands(0).Columns("CountryID").Header.Caption = "Country"
                cmb.DisplayLayout.Bands(0).Columns("CountryID").Hidden = True
                e.Layout.Bands(0).Columns("CountryID").EditorComponent = Me.cmb
                e.Layout.Bands(0).Columns("CountryID").Style = ColumnStyle.DropDownList
                e.Layout.Bands(0).Columns("CountryID").PerformAutoResize(PerformAutoSizeType.AllRowsInBand, False)
                'cmb.Width = e.Layout.Bands(0).Columns("CountryID"-.Width
                'e.Layout.Bands(0).Columns("CountryID").Width = e.Layout.Bands(0).Columns("CountryID").Width - autoWidth

            Case "country"
                e.Layout.Bands(0).Columns("RegionID").Header.Caption = "Region"
                cmb.DisplayLayout.Bands(0).Columns("RegionID").Hidden = True
                e.Layout.Bands(0).Columns("RegionID").EditorComponent = Me.cmb
                e.Layout.Bands(0).Columns("RegionID").Style = ColumnStyle.DropDownList
                e.Layout.Bands(0).Columns("RegionID").PerformAutoResize(PerformAutoSizeType.AllRowsInBand, False)
                cmb.Width = e.Layout.Bands(0).Columns("RegionID").Width

            Case "exchange"
                e.Layout.Bands(0).Columns("Exchange").PerformAutoResize(PerformAutoSizeType.AllRowsInBand, False)

            Case "fund type"
                e.Layout.Bands(0).Columns("FundTypeDescription").PerformAutoResize(PerformAutoSizeType.AllRowsInBand, False)


            Case "head investor"
                e.Layout.Bands(0).Columns("HeadInvestor").PerformAutoResize(PerformAutoSizeType.AllRowsInBand, False)

            Case "industry"
                e.Layout.Bands(0).Columns("Industry").PerformAutoResize(PerformAutoSizeType.AllRowsInBand, False)

            Case "master investor type"
                e.Layout.Bands(0).Columns("MInvestorType").PerformAutoResize(PerformAutoSizeType.AllRowsInBand, False)

            Case "metro"

            Case "region"
                e.Layout.Bands(0).Columns("Region").PerformAutoResize(PerformAutoSizeType.AllRowsInBand, False)

            Case "sector"
                e.Layout.Bands(0).Columns("Sector").PerformAutoResize(PerformAutoSizeType.AllRowsInBand, False)

            Case "state"
                e.Layout.Bands(0).Columns("CountryID").Hidden = False
                e.Layout.Bands(0).Columns("CountryID").Header.Caption = "Country"
                cmb.DisplayLayout.Bands(0).Columns("CountryID").Hidden = True
                e.Layout.Bands(0).Columns("CountryID").EditorComponent = Me.cmb
                e.Layout.Bands(0).Columns("CountryID").Style = ColumnStyle.DropDownList
                e.Layout.Bands(0).Columns("CountryID").PerformAutoResize(PerformAutoSizeType.AllRowsInBand, False)
                e.Layout.Bands(0).Columns("CountryID").Width = e.Layout.Bands(0).Columns("CountryID").Width + autoWidth

            Case "style"
                e.Layout.Bands(0).Columns("Style").PerformAutoResize(PerformAutoSizeType.AllRowsInBand, False)

            Case "territory"
                e.Layout.Bands(0).Columns("Territory").PerformAutoResize(PerformAutoSizeType.AllRowsInBand, False)

            Case "ultimate parent"
                e.Layout.Bands(0).Columns("UltimateParent").PerformAutoResize(PerformAutoSizeType.AllRowsInBand, False)

            Case "investor type linking"
                e.Layout.Bands(0).Columns("InvestorTypeID").Header.Caption = "Investor Type"
                cmb1.DisplayLayout.Bands(0).Columns("InvestorTypeID").Hidden = True
                e.Layout.Bands(0).Columns("InvestorTypeID").EditorComponent = Me.cmb1
                e.Layout.Bands(0).Columns("InvestorTypeID").Style = ColumnStyle.DropDownList
                'e.Layout.Bands(0).Columns("MInvestorTypeID").PerformAutoResize(PerformAutoSizeType.AllRowsInBand, False)
                cmb1.Width = e.Layout.Bands(0).Columns("InvestorTypeID").Width

                e.Layout.Bands(0).Columns("MInvestorTypeID").Header.Caption = "Master Investor Type"
                cmb.DisplayLayout.Bands(0).Columns("MInvestorTypeID").Hidden = True
                e.Layout.Bands(0).Columns("MInvestorTypeID").EditorComponent = Me.cmb
                e.Layout.Bands(0).Columns("MInvestorTypeID").Style = ColumnStyle.DropDownList
                'e.Layout.Bands(0).Columns("MInvestorTypeID").PerformAutoResize(PerformAutoSizeType.AllRowsInBand, False)
                cmb.Width = e.Layout.Bands(0).Columns("MInvestorTypeID").Width

        End Select
    End Sub

    Private Sub grdRecords_InitializeRow(ByVal sender As Object, ByVal e As Infragistics.Win.UltraWinGrid.InitializeRowEventArgs) Handles grdRecords.InitializeRow
        If CheckValidationForm Then
            If e.Row.Cells(0).Value.ToString = String.Empty Then
                e.Row.Appearance.BackColor = Color.Red
            End If
        End If
        e.Row.Cells("Delete").Value = Crystal.CommonDBManagement.My.Resources.delete_ico
        grdRecords.UpdateData()
    End Sub

    Private Function CheckBeforeDeletion() As Boolean
        Dim eReturnVal As New ExecutionResult
        Dim dtSet As New DataSet
        Dim key As String = ""
        Dim objAddEditLKPTable As cAddEditLKPTable = Nothing
        Try
            objAddEditLKPTable = New cAddEditLKPTable
            If combo Is Nothing Then
                combo = ""
            End If
            If (grdRecords.ActiveRow.Cells(0).Text.Trim() <> "") Then
                eReturnVal = objAddEditLKPTable.CheckRecordExistence(cmbMasterTable.SelectedValue, grdRecords.ActiveRow.Cells(0).Value)
                dtSet = CType(eReturnVal.ResultObject, DataSet)
                If (dtSet IsNot Nothing AndAlso dtSet.Tables(0).Rows().Count > 0) Then
                    Return True
                Else
                    ShowMessageBox("This record has now been deleted and can no longer be modified")
                    Return False
                End If
            End If
            Return True
        Catch ex As Exception
            If (Not Me.IsDisposed) Then
                MessageBox.Show(ex.Message)
            End If
        End Try
    End Function

    ''' <summary>
    ''' get the reference column names
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function getReferenceTableColumnInfo() As DataSet
        Dim eReturnVal As New ExecutionResult
        Dim objReferenceTableInfo As New cAddEditLKPTable
        Dim dtSet As New DataSet
        Try
            eReturnVal = objReferenceTableInfo.SelectReferenceTableColumnInfo(cmbMasterTable.SelectedValue)
            dtSet = CType(eReturnVal.ResultObject, DataSet)
            Return dtSet
        Catch ex As Exception
            If (Not Me.IsDisposed) Then
                MessageBox.Show(ex.Message)
            End If
            Return Nothing
        Finally
            eReturnVal = Nothing
            objReferenceTableInfo = Nothing
            dtSet = Nothing
        End Try
    End Function

    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
        Me.Close()
    End Sub

    Private Sub btnAddRecord_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAddRecord.Click
        'ButtonAddNewEvent = True
        AddNewRowInGrid()
        'ButtonAddNewEvent = False
    End Sub

    Private Sub AddDataToDataTable()
        Dim dtRecords As New DataTable
        Dim newRow As DataRow
        NotSavedData = New DataTable
        grdRecords.Refresh()
        dtRecords = CType(grdRecords.DataSource, DataTable)
        If dtRecords.Columns(1).ColumnName = "City" Or (dtRecords.Columns(1).ColumnName = "InvestorTypeID" And dtRecords.Columns(2).ColumnName = "MInvestorTypeID") Then
            NotSavedData.Columns.Add(dtRecords.Columns(1).ColumnName, GetType(String))
            NotSavedData.Columns.Add(dtRecords.Columns(2).ColumnName, GetType(String))
        End If
        For Each row As DataRow In dtRecords.Rows
            If dtRecords.Columns(1).ColumnName = "City" Then
                If Not CommonCLL.blnRowBlank(row) Then
                    Dim value As String = row(0).ToString
                    If value = String.Empty Then
                        If row("CountryID").ToString <> String.Empty Then
                            dataTable.ImportRow(row)
                        Else
                            newRow = NotSavedData.NewRow()
                            newRow("City") = row("City")
                            newRow("CountryID") = row("CountryID")
                            NotSavedData.Rows.Add(newRow)
                        End If
                    End If
                End If
            ElseIf dtRecords.Columns(1).ColumnName = "InvestorTypeID" And dtRecords.Columns(2).ColumnName = "MInvestorTypeID" Then
                If Not CommonCLL.blnRowBlank(row) Then
                    Dim value As String = row(0).ToString
                    If value = String.Empty Then
                        If row("MInvestorTypeID").ToString <> String.Empty Then
                            dataTable.ImportRow(row)
                        Else
                            newRow = NotSavedData.NewRow()
                            newRow("InvestorTypeID") = row("InvestorTypeID")
                            newRow("MInvestorTypeID") = row("MInvestorTypeID")
                            NotSavedData.Rows.Add(newRow)
                        End If
                    End If
                End If
            Else
                If Not CommonCLL.blnRowBlank(row) Then
                    Dim value As String = row(0).ToString
                    If value = String.Empty Then
                        dataTable.ImportRow(row)
                    End If
                End If
            End If

        Next
    End Sub

    Private Sub FillDataTable()
        dataTable = New DataTable
        Select Case combo.Trim().ToLower
            Case "activist"
                dataTable.Columns.Add("ActivistCategory", GetType(String))
                dataTable.Columns.Add("ActivistCode", GetType(String))
            Case "city"
                dataTable.Columns.Add("City", GetType(String))
                dataTable.Columns.Add("CountryID", GetType(Integer))
            Case "country"
                dataTable.Columns.Add("CountryCode", GetType(String))
                dataTable.Columns.Add("CountryName", GetType(String))
                dataTable.Columns.Add("RegionID", GetType(Integer))
            Case "exchange"
                dataTable.Columns.Add("Exchange", GetType(String))
            Case "fund type"
                dataTable.Columns.Add("FundType", GetType(String))
                dataTable.Columns.Add("FundTypeDescription", GetType(String))
            Case "head investor"
                dataTable.Columns.Add("HeadInvestor", GetType(String))
            Case "industry"
                dataTable.Columns.Add("Industry", GetType(String))
            Case "investor type"
                dataTable.Columns.Add("InvestorType", GetType(String))
                dataTable.Columns.Add("InvestorTypeCode", GetType(String))
                dataTable.Columns.Add("IsExcludedFromSearch", GetType(Boolean))

            Case "metro"
                dataTable.Columns.Add("Metro", GetType(String))

            Case "region"
                dataTable.Columns.Add("RegionCode", GetType(String))
                dataTable.Columns.Add("Region", GetType(String))

            Case "sector"
                dataTable.Columns.Add("Sector", GetType(String))

            Case "state"
                dataTable.Columns.Add("StateCode", GetType(String))
                dataTable.Columns.Add("State", GetType(String))
                dataTable.Columns.Add("CountryID", GetType(Integer))

            Case "territory"
                dataTable.Columns.Add("territory", GetType(String))

            Case "ultimate parent"
                dataTable.Columns.Add("UltimateParent", GetType(String))

            Case "master investor type"
                dataTable.Columns.Add("MInvestorType", GetType(String))
                dataTable.Columns.Add("MInvestorTypeCode", GetType(String))

            Case "style"
                dataTable.Columns.Add("Style", GetType(String))

            Case "turnover"
                dataTable.Columns.Add("Turnover", GetType(String))

            Case "investor type linking"
                dataTable.Columns.Add("InvestorTypeID", GetType(Integer))
                dataTable.Columns.Add("MInvestorTypeID", GetType(Integer))

        End Select

    End Sub
    Private Sub ValidateRow(ByVal row As DataRow, ByRef msg As String)
        Dim message As String = ""
        Select Case combo.Trim().ToLower
            Case "activist"
                If (row("ActivistCategory").ToString.Trim() = "") Then
                    'row("ActivistCategory").Activate()
                    'grdRecords.PerformAction(UltraGridAction.EnterEditMode)
                    message = "Please enter the activist category."
                End If
            Case "city"
                If (row("City").ToString.Trim() = "") Then
                    'row("City").Activate()
                    'grdRecords.PerformAction(UltraGridAction.EnterEditMode)
                    message = "Please enter the city."
                ElseIf (row("CountryID").ToString.Trim() = "") Then
                    'row("CountryID").Activate()
                    'grdRecords.PerformAction(UltraGridAction.EnterEditMode)
                    message = "Please select the country."
                End If
            Case "country"
                If (row("CountryCode").ToString.Trim() = "") Then
                    'row("CountryCode").Activate()
                    'grdRecords.PerformAction(UltraGridAction.EnterEditMode)
                    message = "Please enter the country code."
                ElseIf (row("CountryName").ToString.Trim() = "") Then
                    'row("CountryName").Activate()
                    'grdRecords.PerformAction(UltraGridAction.EnterEditMode)
                    message = "Please enter the country name."
                ElseIf (row("RegionID").ToString.Trim() = "") Then
                    'row("RegionID").Activate()
                    'grdRecords.PerformAction(UltraGridAction.EnterEditMode)
                    message = "Please select the region."
                End If

            Case "exchange"
                If (row("Exchange").ToString.Trim() = "") Then
                    'row("Exchange").Activate()
                    'grdRecords.PerformAction(UltraGridAction.EnterEditMode)
                    message = "Please select the exchange."
                End If

            Case "fund type"
                If (row("FundType").ToString.Trim() = "") Then
                    'row("FundType").Activate()
                    'grdRecords.PerformAction(UltraGridAction.EnterEditMode)
                    message = "Please enter the fund type."
                ElseIf (row("FundTypeDescription").ToString.Trim() = "") Then
                    'row("FundTypeDescription").Activate()
                    'grdRecords.PerformAction(UltraGridAction.EnterEditMode)
                    message = "Please enter the fund type description."
                End If

            Case "head investor"
                If (row("HeadInvestor").ToString.Trim() = "") Then
                    'row("HeadInvestor").Activate()
                    'grdRecords.PerformAction(UltraGridAction.EnterEditMode)
                    message = "Please select the head investor."
                End If

            Case "industry"
                If (row("Industry").ToString.Trim() = "") Then
                    'row("Industry").Activate()
                    'grdRecords.PerformAction(UltraGridAction.EnterEditMode)
                    message = "Please enter the industry."
                End If

            Case "investor type"
                If (row("InvestorType").ToString.Trim() = "") Then
                    'row("InvestorType").Activate()
                    'grdRecords.PerformAction(UltraGridAction.EnterEditMode)
                    message = "Please enter the investor type."
                End If
                If (row("IsExcludedFromSearch").ToString = String.Empty) Then
                    'row("IsExcludedFromSearch").Activate()
                    'grdRecords.PerformAction(UltraGridAction.EnterEditMode)
                    message = "Please check or uncheck the box."
                End If

            Case "metro"
                If (row("Metro").ToString.Trim() = "") Then
                    'row("Metro").Activate()
                    'grdRecords.PerformAction(UltraGridAction.EnterEditMode)
                    message = "Please enter the metro."
                End If

            Case "region"
                If (row("RegionCode").ToString.Trim() = "") Then
                    'row("RegionCode").Activate()
                    'grdRecords.PerformAction(UltraGridAction.EnterEditMode)
                    message = "Please enter the region code."
                ElseIf (row("Region").ToString.Trim() = "") Then
                    'row("Region").Activate()
                    'grdRecords.PerformAction(UltraGridAction.EnterEditMode)
                    message = "Please enter the region."
                End If

            Case "sector"
                If (row("Sector").ToString.Trim() = "") Then
                    'row("Sector").Activate()
                    'grdRecords.PerformAction(UltraGridAction.EnterEditMode)
                    message = "Please select the sector."
                End If

            Case "state"
                If (row("StateCode").ToString.Trim() = "") Then
                    'row("StateCode").Activate()
                    'grdRecords.PerformAction(UltraGridAction.EnterEditMode)
                    message = "Please enter the state code."
                ElseIf (row("State").ToString.Trim() = "") Then
                    'row("State").Activate()
                    'grdRecords.PerformAction(UltraGridAction.EnterEditMode)
                    message = "Please enter the state."
                ElseIf (row("CountryID").ToString.Trim() = "") Then
                    'row("CountryID").Activate()
                    'grdRecords.PerformAction(UltraGridAction.EnterEditMode)
                    message = "Please select the country."
                End If

            Case "style"
                If (row("Style").ToString.Trim() = "") Then
                    'row("Style").Activate()
                    'grdRecords.PerformAction(UltraGridAction.EnterEditMode)
                    message = "Please select the style."
                End If

            Case "territory"
                If (row("Territory").ToString.Trim() = "") Then
                    'row("Territory").Activate()
                    'grdRecords.PerformAction(UltraGridAction.EnterEditMode)
                    message = "Please enter the territory."
                End If

            Case "ultimate parent"
                If (row("UltimateParent").ToString.Trim() = "") Then
                    'row("UltimateParent").Activate()
                    'grdRecords.PerformAction(UltraGridAction.EnterEditMode)
                    message = "Please select the ultimate parent."
                End If

            Case "turnover"
                If (row("Turnover").ToString.Trim() = "") Then
                    'row("Turnover").Activate()
                    'grdRecords.PerformAction(UltraGridAction.EnterEditMode)
                    message = "Please enter the turnover."
                End If

            Case "master investor type"
                If (row("MInvestorType").ToString.Trim() = "") Then
                    'row("MInvestorType").Activate()
                    'grdRecords.PerformAction(UltraGridAction.EnterEditMode)
                    message = "Please enter the master investor type."
                End If

            Case "investor type linking"
                If (row("InvestorTypeID").ToString.Trim() = "") Then
                    'row("InvestorTypeID").Activate()
                    'grdRecords.PerformAction(UltraGridAction.EnterEditMode)
                    message = "Please select the investor type."
                End If
                If (row("MInvestorTypeID").ToString.ToString = "") Then
                    'row("MInvestorTypeID").Activate()
                    'grdRecords.PerformAction(UltraGridAction.EnterEditMode)
                    message = "Please select the master investor type."
                End If

        End Select
        msg = message
    End Sub
    Private Function Validation(ByRef msg As String) As Boolean
        Dim objmaster As cAddEditLKPTable = Nothing
        Dim message As String = ""
        Try
            objmaster = New cAddEditLKPTable
            If (grdRecords.Rows.Count = 0) Then
                MessageBox.Show("Please select the record", m_strMsgBoxCaption, MessageBoxButtons.OK, MessageBoxIcon.Information)
                Return False
            End If
            If (grdRecords.ActiveRow Is Nothing) Then
                MessageBox.Show("Please select the record", m_strMsgBoxCaption, MessageBoxButtons.OK, MessageBoxIcon.Information)
                Return False
            End If

            Select Case combo.Trim().ToLower
                Case "activist"
                    If (grdRecords.ActiveRow.Cells("ActivistCategory").Text.Trim() = "") Then
                        grdRecords.ActiveRow.Cells("ActivistCategory").Activate()
                        grdRecords.PerformAction(UltraGridAction.EnterEditMode)
                        message = "Please enter the activist category."
                    End If
                Case "city"
                    If (grdRecords.ActiveRow.Cells("City").Text.Trim() = "") Then
                        grdRecords.ActiveRow.Cells("City").Activate()
                        grdRecords.PerformAction(UltraGridAction.EnterEditMode)
                        message = "Please enter the city."
                    ElseIf (grdRecords.ActiveRow.Cells("CountryID").Text.Trim() = "") Then
                        grdRecords.ActiveRow.Cells("CountryID").Activate()
                        grdRecords.PerformAction(UltraGridAction.EnterEditMode)
                        message = "Please select the country."
                    End If

                Case "country"
                    If (grdRecords.ActiveRow.Cells("CountryCode").Text.Trim() = "") Then
                        grdRecords.ActiveRow.Cells("CountryCode").Activate()
                        grdRecords.PerformAction(UltraGridAction.EnterEditMode)
                        message = "Please enter the country code."
                    ElseIf (grdRecords.ActiveRow.Cells("CountryName").Text.Trim() = "") Then
                        grdRecords.ActiveRow.Cells("CountryName").Activate()
                        grdRecords.PerformAction(UltraGridAction.EnterEditMode)
                        message = "Please enter the country name."
                    ElseIf (grdRecords.ActiveRow.Cells("RegionID").Text.Trim() = "") Then
                        grdRecords.ActiveRow.Cells("RegionID").Activate()
                        grdRecords.PerformAction(UltraGridAction.EnterEditMode)
                        message = "Please select the region."
                    End If

                Case "exchange"
                    If (grdRecords.ActiveRow.Cells("Exchange").Text.Trim() = "") Then
                        grdRecords.ActiveRow.Cells("Exchange").Activate()
                        grdRecords.PerformAction(UltraGridAction.EnterEditMode)
                        message = "Please select the exchange."
                    End If

                Case "fund type"
                    If (grdRecords.ActiveRow.Cells("FundType").Text.Trim() = "") Then
                        grdRecords.ActiveRow.Cells("FundType").Activate()
                        grdRecords.PerformAction(UltraGridAction.EnterEditMode)
                        message = "Please enter the fund type."
                    ElseIf (grdRecords.ActiveRow.Cells("FundTypeDescription").Text.Trim() = "") Then
                        grdRecords.ActiveRow.Cells("FundTypeDescription").Activate()
                        grdRecords.PerformAction(UltraGridAction.EnterEditMode)
                        message = "Please enter the fund type description."
                    End If

                Case "head investor"
                    If (grdRecords.ActiveRow.Cells("HeadInvestor").Text.Trim() = "") Then
                        grdRecords.ActiveRow.Cells("HeadInvestor").Activate()
                        grdRecords.PerformAction(UltraGridAction.EnterEditMode)
                        message = "Please select the head investor."
                    End If

                Case "industry"
                    If (grdRecords.ActiveRow.Cells("Industry").Text.Trim() = "") Then
                        grdRecords.ActiveRow.Cells("Industry").Activate()
                        grdRecords.PerformAction(UltraGridAction.EnterEditMode)
                        message = "Please enter the industry."
                    End If

                Case "investor type"
                    If (grdRecords.ActiveRow.Cells("InvestorType").Text.Trim() = "") Then
                        grdRecords.ActiveRow.Cells("InvestorType").Activate()
                        grdRecords.PerformAction(UltraGridAction.EnterEditMode)
                        message = "Please enter the investor type."
                    End If
                    If (grdRecords.ActiveRow.Cells("IsExcludedFromSearch").Value.ToString = String.Empty) Then
                        grdRecords.ActiveRow.Cells("IsExcludedFromSearch").Activate()
                        grdRecords.PerformAction(UltraGridAction.EnterEditMode)
                        message = "Please check or uncheck the box."
                    End If

                Case "metro"
                    If (grdRecords.ActiveRow.Cells("Metro").Text.Trim() = "") Then
                        grdRecords.ActiveRow.Cells("Metro").Activate()
                        grdRecords.PerformAction(UltraGridAction.EnterEditMode)
                        message = "Please enter the metro."
                    End If

                Case "region"
                    If (grdRecords.ActiveRow.Cells("RegionCode").Text.Trim() = "") Then
                        grdRecords.ActiveRow.Cells("RegionCode").Activate()
                        grdRecords.PerformAction(UltraGridAction.EnterEditMode)
                        message = "Please enter the region code."
                    ElseIf (grdRecords.ActiveRow.Cells("Region").Text.Trim() = "") Then
                        grdRecords.ActiveRow.Cells("Region").Activate()
                        grdRecords.PerformAction(UltraGridAction.EnterEditMode)
                        message = "Please enter the region."
                    End If

                Case "sector"
                    If (grdRecords.ActiveRow.Cells("Sector").Text.Trim() = "") Then
                        grdRecords.ActiveRow.Cells("Sector").Activate()
                        grdRecords.PerformAction(UltraGridAction.EnterEditMode)
                        message = "Please select the sector."
                    End If

                Case "state"
                    If (grdRecords.ActiveRow.Cells("StateCode").Text.Trim() = "") Then
                        grdRecords.ActiveRow.Cells("StateCode").Activate()
                        grdRecords.PerformAction(UltraGridAction.EnterEditMode)
                        message = "Please enter the state code."
                    ElseIf (grdRecords.ActiveRow.Cells("State").Text.Trim() = "") Then
                        grdRecords.ActiveRow.Cells("State").Activate()
                        grdRecords.PerformAction(UltraGridAction.EnterEditMode)
                        message = "Please enter the state."
                    ElseIf (grdRecords.ActiveRow.Cells("CountryID").Text.Trim() = "") Then
                        grdRecords.ActiveRow.Cells("CountryID").Activate()
                        grdRecords.PerformAction(UltraGridAction.EnterEditMode)
                        message = "Please select the country."
                    End If

                Case "style"
                    If (grdRecords.ActiveRow.Cells("Style").Text.Trim() = "") Then
                        grdRecords.ActiveRow.Cells("Style").Activate()
                        grdRecords.PerformAction(UltraGridAction.EnterEditMode)
                        message = "Please select the style."
                    End If

                Case "territory"
                    If (grdRecords.ActiveRow.Cells("Territory").Text.Trim() = "") Then
                        grdRecords.ActiveRow.Cells("Territory").Activate()
                        grdRecords.PerformAction(UltraGridAction.EnterEditMode)
                        message = "Please enter the territory."
                    End If

                Case "ultimate parent"
                    If (grdRecords.ActiveRow.Cells("UltimateParent").Text.Trim() = "") Then
                        grdRecords.ActiveRow.Cells("UltimateParent").Activate()
                        grdRecords.PerformAction(UltraGridAction.EnterEditMode)
                        message = "Please select the ultimate parent."
                    End If

                Case "turnover"
                    If (grdRecords.ActiveRow.Cells("Turnover").Text.Trim() = "") Then
                        grdRecords.ActiveRow.Cells("Turnover").Activate()
                        grdRecords.PerformAction(UltraGridAction.EnterEditMode)
                        message = "Please enter the turnover."
                    End If

                Case "master investor type"
                    If (grdRecords.ActiveRow.Cells("MInvestorType").Text.Trim() = "") Then
                        grdRecords.ActiveRow.Cells("MInvestorType").Activate()
                        grdRecords.PerformAction(UltraGridAction.EnterEditMode)
                        message = "Please enter the master investor type."
                    End If

                Case "investor type linking"
                    If (grdRecords.ActiveRow.Cells("InvestorTypeID").Text.Trim() = "") Then
                        grdRecords.ActiveRow.Cells("InvestorTypeID").Activate()
                        grdRecords.PerformAction(UltraGridAction.EnterEditMode)
                        message = "Please select the investor type."
                    End If
                    If (grdRecords.ActiveRow.Cells("MInvestorTypeID").Text.ToString = "") Then
                        grdRecords.ActiveRow.Cells("MInvestorTypeID").Activate()
                        grdRecords.PerformAction(UltraGridAction.EnterEditMode)
                        message = "Please select the master investor type."
                    End If

            End Select
            msg = message
            If message = String.Empty Then
                Return True
            ElseIf message <> String.Empty Then
                'MessageBox.Show(message)
                Return False
            End If
        Catch ex As Exception
            If (Not Me.IsDisposed) Then
                MessageBox.Show(ex.Message)
            End If
        Finally
            objmaster = Nothing
            message = ""
        End Try
        Return True
    End Function

    Private Sub AddNewRowInGrid()
        If grdRecords Is Nothing Then
            FillDataTable()
            grdRecords.DataSource = dataTable
        Else
            If grdRecords.ActiveRow IsNot Nothing Then
                If Validation(msg) Then
                    grdRecords.DisplayLayout.Bands(0).AddNew()
                    grdRecords.DisplayLayout.Rows(grdRecords.Rows.Count - 1).Cells(1).Activate()
                    grdRecords.DisplayLayout.Rows(grdRecords.Rows.Count - 1).Appearance.BackColor = Color.Red
                Else
                    MessageBox.Show(msg, BaseClass.crystalCaption, MessageBoxButtons.OK, MessageBoxIcon.Information)
                    msg = String.Empty
                End If
            Else
                grdRecords.DisplayLayout.Bands(0).AddNew()
                grdRecords.DisplayLayout.Rows(grdRecords.Rows.Count - 1).Cells(1).Activate()
            End If
        End If
    End Sub

    Private Sub AddNewRecord()
        Try
            If (grdRecords.Rows.Count > 0) Then
                'If (grdRecords.DisplayLayout.Rows(grdRecords.Rows.Count - 1).Cells(0).Text <> "") Then
                '    grdRecords.DisplayLayout.Bands(0).AddNew()
                '    grdRecords.DisplayLayout.Rows(grdRecords.Rows.Count - 1).Cells(1).Activate()
                '    grdRecords.DisplayLayout.Rows(grdRecords.Rows.Count - 1).Appearance.BackColor = Color.Red
                '    Exit Sub
                'End If
                If Validation(msg) Then
                    If dataTable Is Nothing Then
                        FillDataTable()
                        '                    Exit Sub
                    End If
                    AddDataToDataTable()
                    'If AddUpdateRecord() Then
                    '    lblTotalRecords.Text = "Total records:- " + grdRecords.Rows.Count.ToString()
                    '    AddNewRecord()
                    'End If
                    'Else
                    '    ShowMessageBox(DBResult.RECORD_CREATED)
                    '    Exit Sub
                    'End If
                    'Else
                    'grdRecords.DisplayLayout.Bands(0).AddNew()
                    'grdRecords.DisplayLayout.Rows(grdRecords.Rows.Count - 1).Cells(1).Activate()
                    'grdRecords.DisplayLayout.Rows(grdRecords.Rows.Count - 1).Appearance.BackColor = Color.Red
                End If
            Else
                'TempDt = New DataTable()
                'TempDt.Rows.Clear()
                grdRecords.DataSource = TempDt
                'hidecolumnsofgrid()

                grdRecords.DisplayLayout.Bands(0).AddNew()
            End If
            If combo Is Nothing Then
                combo = ""
            End If

        Catch ex As Exception
            If (Not Me.IsDisposed) Then
                MessageBox.Show(ex.Message)
            End If
        End Try

    End Sub

    Private Sub BindCountryWithCmb()
        Dim eReturnVal As New ExecutionResult
        Dim objReferenceTable As New cAddEditLKPTable
        Dim dtSet As New DataSet
        Try
            'cmb.DataMember = "CountryName"
            cmb.ValueMember = "CountryID"
            cmb.DisplayMember = "CountryName"
            eReturnVal = objReferenceTable.LoadAllCountries()
            dtSet = CType(eReturnVal.ResultObject, DataSet)
            cmb.DataSource = dtSet.Tables(0)
        Catch ex As Exception
            If (Not Me.IsDisposed) Then
                MessageBox.Show(ex.Message)
            End If
        Finally
            eReturnVal = Nothing
            objReferenceTable = Nothing
            dtSet = Nothing
        End Try
    End Sub

    Private Sub BindMasterTypeWithCmb()
        Dim eReturnVal As New ExecutionResult
        Dim objReferenceTable As New cAddEditLKPTable
        Dim dtSet As New DataSet
        Try
            cmb.ValueMember = "MInvestorTypeID"
            cmb.DisplayMember = "MInvestorType"
            eReturnVal = objReferenceTable.LoadAllMasterType()
            dtSet = CType(eReturnVal.ResultObject, DataSet)
            cmb.DataSource = dtSet.Tables(0)
        Catch ex As Exception
            If (Not Me.IsDisposed) Then
                MessageBox.Show(ex.Message)
            End If
        Finally
            eReturnVal = Nothing
            objReferenceTable = Nothing
            dtSet = Nothing
        End Try
    End Sub

    Private Sub BindInvestorTypeWithCmb()
        Dim eReturnVal As New ExecutionResult
        Dim objReferenceTable As New cAddEditLKPTable
        Dim dtSet As New DataSet
        Try
            cmb1.ValueMember = "InvestorTypeID"
            cmb1.DisplayMember = "InvestorType"
            eReturnVal = objReferenceTable.LoadAllInvestorType()
            dtSet = CType(eReturnVal.ResultObject, DataSet)
            cmb1.DataSource = dtSet.Tables(0)
        Catch ex As Exception
            If (Not Me.IsDisposed) Then
                MessageBox.Show(ex.Message)
            End If
        Finally
            eReturnVal = Nothing
            objReferenceTable = Nothing
            dtSet = Nothing
        End Try
    End Sub

    Private Sub BindRegionWithCmb()
        Dim eReturnVal As New ExecutionResult
        Dim objReferenceTable As New cAddEditLKPTable
        Dim dtSet As New DataSet
        Try
            'cmb.DataMember = "CountryName"
            cmb.ValueMember = "RegionID"
            cmb.DisplayMember = "Region"
            eReturnVal = objReferenceTable.LoadAllRegions()
            dtSet = CType(eReturnVal.ResultObject, DataSet)
            cmb.DataSource = dtSet.Tables(0)
        Catch ex As Exception
            If (Not Me.IsDisposed) Then
                MessageBox.Show(ex.Message)
            End If
        Finally
            eReturnVal = Nothing
            objReferenceTable = Nothing
            dtSet = Nothing
        End Try
    End Sub

    Private Sub BindComboInCell()
        Dim eReturnVal As New ExecutionResult
        Dim objReferenceTable As New cAddEditLKPTable
        Dim dtSet As New DataSet
        Try
            If cmbMasterTable.Text.ToLower().ToString = "city" Or cmbMasterTable.Text.ToLower().ToString = "state" Then
                BindCountryWithCmb()
            ElseIf cmbMasterTable.Text.ToLower().ToString = "country" Then
                BindRegionWithCmb()
                'ElseIf cmbMasterTable.Text.ToLower().ToString = "investor type" Then
                '    BindMasterTypeWithCmb()
            ElseIf cmbMasterTable.Text.ToLower().ToString.Trim = "investor type linking" Then
                BindMasterTypeWithCmb()
                BindInvestorTypeWithCmb()
            End If
        Catch ex As Exception
            If (Not Me.IsDisposed) Then
                MessageBox.Show(ex.Message)
            End If
        Finally
            eReturnVal = Nothing
            objReferenceTable = Nothing
            dtSet = Nothing
        End Try
    End Sub

    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        Dim grdRecordsSelected As DataTable
        Dim m_pRow As DataRow = Nothing
        If grdRecords.ActiveRow Is Nothing Then
            MessageBox.Show("Please select the row.", "Crystal", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Exit Sub
        End If
        grdRecords.Selected.Rows.Add(grdRecords.ActiveRow)
        If grdRecords.Selected.Rows.Count > 0 Then
            grdRecordsSelected = CType(grdRecords.DataSource, DataTable).Clone
            For Each gridRow As UltraGridRow In grdRecords.Selected.Rows
                m_pRow = grdRecordsSelected.NewRow()
                m_pRow.ItemArray = CType(gridRow.ListObject, DataRowView).Row.ItemArray
                If m_pRow.Item(0).ToString = String.Empty Then
                    gridRow.Delete(False)
                    Continue For
                End If
                grdRecordsSelected.Rows.Add(m_pRow)
            Next
            If grdRecordsSelected.Rows.Count > 0 Then
                Dim delete = New frmDeleteLKPTable()
                delete.Datatable = grdRecordsSelected
                delete.TableID = cmbMasterTable.SelectedValue
                AddHandler delete.deleteRecord, AddressOf RaiseEventForDelete
                delete.ShowDialog()
            End If

        End If
    End Sub

    Public Sub RaiseEventForDelete(ByVal dt As DataTable, ByVal isDeleted As Boolean)
        If (Me.IsDisposed) Then Exit Sub
        Dim dDtgrdRecords As DataTable
        Dim indexRow As Integer
        dDtgrdRecords = CType(grdRecords.DataSource, DataTable)
        For Each row As DataRow In dt.Rows
            'dDtgrdRecords.Rows.Remove(row)
            If isDeleted Then
                indexRow = FindRowIndex(dDtgrdRecords, dDtgrdRecords.Columns(0).ColumnName.ToString, row(0).ToString())
                dDtgrdRecords.Rows(indexRow).Delete()
                dDtgrdRecords.AcceptChanges()
            End If
        Next

        'Dim dv As New DataView(dDtgrdRecords)
        'If combo = "City" Then
        '    dv.Sort = "[" + dDtgrdRecords.Columns(2).ColumnName + "," + dDtgrdRecords.Columns(1).ColumnName + "] asc"
        'Else
        '    dv.Sort = "[" + dDtgrdRecords.Columns(0).ColumnName + "] asc"
        'End If
        'grdRecords.DataSource = dv.ToTable()
        lblTotalRecords.Text = "Total records:- " + grdRecords.Rows.Count.ToString()
        'grdRecords.ActiveRow = Nothing
    End Sub

    Private Sub grdRecords_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles grdRecords.KeyDown
        If grdRecords.ActiveRow Is Nothing Then Exit Sub
        If e.KeyValue = Keys.Escape Then
            If grdRecords.ActiveRow.Cells(0).Value.ToString <> String.Empty Then
                If grdRecords.ActiveCell IsNot Nothing Then
                    grdRecords.ActiveCell.CancelUpdate()
                End If
            Else
                grdRecords.UpdateData()
            End If
        End If
    End Sub

    Protected Function CreateListingXML(ByVal ldtProjects As DataTable) As StringBuilder
        Dim i As Integer
        Dim Logmessage As New System.Text.StringBuilder

        'Dim ldtProjects As DataTable
        Dim sbXML As New StringBuilder
        sbXML.Append("<?xml version=" & """1.0" & """ encoding=" & """Windows-1252" & """?>")
        Try
            If ldtProjects.Rows.Count > 0 Then
                ' ldtProjects = (CType(grdClientDetail.DataSource, DataTable))
                sbXML.Append("<Root>")
                For i = 0 To ldtProjects.Rows.Count - 1
                    If Not CommonCLL.blnRowBlank(ldtProjects.Rows(i)) Then
                        Select Case combo.Trim.ToLower
                            Case "activist"
                                sbXML.Append("<Row ActivistCategory=""" & ldtProjects.Rows(i)(0).ToString().Replace("'", "&apos;").Replace("&", "&amp;").Replace("<", "&lt;").Replace(">", "&gt;").Replace(ControlChars.Quote, "&quot;") & """ " & _
                                 "ActivistCode=""" & ldtProjects.Rows(i)(1).ToString().Replace("'", "&apos;").Replace("&", "&amp;").Replace("<", "&lt;").Replace(">", "&gt;").Replace(ControlChars.Quote, "&quot;") & """ " & _
                             "/>")
                                sbXML.Append(vbNewLine)

                            Case "city"
                                sbXML.Append("<Row City=""" & ldtProjects.Rows(i)(0).ToString().Replace("'", "&apos;").Replace("&", "&amp;").Replace("<", "&lt;").Replace(">", "&gt;").Replace(ControlChars.Quote, "&quot;") & """ " & _
                                 "CountryID=""" & ldtProjects.Rows(i)(1).ToString().Replace("'", "&apos;").Replace("&", "&amp;").Replace("<", "&lt;").Replace(">", "&gt;").Replace(ControlChars.Quote, "&quot;") & """ " & _
                             "/>")
                                sbXML.Append(vbNewLine)

                            Case "country"
                                sbXML.Append("<Row CountryCode=""" & ldtProjects.Rows(i)(0).ToString().Replace("'", "&apos;").Replace("&", "&amp;").Replace("<", "&lt;").Replace(">", "&gt;").Replace(ControlChars.Quote, "&quot;") & """ " & _
                                 "CountryName=""" & ldtProjects.Rows(i)(1).ToString().Replace("'", "&apos;").Replace("&", "&amp;").Replace("<", "&lt;").Replace(">", "&gt;").Replace(ControlChars.Quote, "&quot;") & """ " & _
                                 "RegionID=""" & ldtProjects.Rows(i)(2).ToString().Replace("'", "&apos;").Replace("&", "&amp;").Replace("<", "&lt;").Replace(">", "&gt;").Replace(ControlChars.Quote, "&quot;") & """ " & _
                             "/>")
                                sbXML.Append(vbNewLine)

                            Case "exchange"
                                sbXML.Append("<Row Exchange=""" & ldtProjects.Rows(i)(0).ToString().Replace("'", "&apos;").Replace("&", "&amp;").Replace("<", "&lt;").Replace(">", "&gt;").Replace(ControlChars.Quote, "&quot;") & """ " & _
                                             "/>")
                                sbXML.Append(vbNewLine)

                            Case "fund type"
                                sbXML.Append("<Row FundType=""" & ldtProjects.Rows(i)(0).ToString().Replace("'", "&apos;").Replace("&", "&amp;").Replace("<", "&lt;").Replace(">", "&gt;").Replace(ControlChars.Quote, "&quot;") & """ " & _
                                 "FundTypeDescription=""" & ldtProjects.Rows(i)(1).ToString().Replace("'", "&apos;").Replace("&", "&amp;").Replace("<", "&lt;").Replace(">", "&gt;").Replace(ControlChars.Quote, "&quot;") & """ " & _
                                "/>")
                                sbXML.Append(vbNewLine)

                            Case "head investor"
                                sbXML.Append("<Row HeadInvestor=""" & ldtProjects.Rows(i)(0).ToString().Replace("'", "&apos;").Replace("&", "&amp;").Replace("<", "&lt;").Replace(">", "&gt;").Replace(ControlChars.Quote, "&quot;") & """ " & _
                                "/>")
                                sbXML.Append(vbNewLine)

                            Case "industry"
                                sbXML.Append("<Row Industry=""" & ldtProjects.Rows(i)(0).ToString().Replace("'", "&apos;").Replace("&", "&amp;").Replace("<", "&lt;").Replace(">", "&gt;").Replace(ControlChars.Quote, "&quot;") & """ " & _
                                "/>")
                                sbXML.Append(vbNewLine)

                            Case "investor type"
                                sbXML.Append("<Row InvestorType=""" & ldtProjects.Rows(i)(0).ToString().Replace("'", "&apos;").Replace("&", "&amp;").Replace("<", "&lt;").Replace(">", "&gt;").Replace(ControlChars.Quote, "&quot;") & """ " & _
                                 "InvestorTypeCode=""" & ldtProjects.Rows(i)(1).ToString().Replace("'", "&apos;").Replace("&", "&amp;").Replace("<", "&lt;").Replace(">", "&gt;").Replace(ControlChars.Quote, "&quot;") & """ " & _
                                 "IsExcludedFromSearch=""" & ldtProjects.Rows(i)(2).ToString().Replace("'", "&apos;").Replace("&", "&amp;").Replace("<", "&lt;").Replace(">", "&gt;").Replace(ControlChars.Quote, "&quot;") & """ " & _
                                "/>")
                                sbXML.Append(vbNewLine)

                            Case "metro"
                                sbXML.Append("<Row Metro=""" & ldtProjects.Rows(i)(0).ToString().Replace("'", "&apos;").Replace("&", "&amp;").Replace("<", "&lt;").Replace(">", "&gt;").Replace(ControlChars.Quote, "&quot;") & """ " & _
                                "/>")
                                sbXML.Append(vbNewLine)

                            Case "region"
                                sbXML.Append("<Row RegionCode=""" & ldtProjects.Rows(i)(0).ToString().Replace("'", "&apos;").Replace("&", "&amp;").Replace("<", "&lt;").Replace(">", "&gt;").Replace(ControlChars.Quote, "&quot;") & """ " & _
                                 "Region=""" & ldtProjects.Rows(i)(1).ToString().Replace("'", "&apos;").Replace("&", "&amp;").Replace("<", "&lt;").Replace(">", "&gt;").Replace(ControlChars.Quote, "&quot;") & """ " & _
                                "/>")
                                sbXML.Append(vbNewLine)

                            Case "sector"
                                sbXML.Append("<Row Sector=""" & ldtProjects.Rows(i)(0).ToString().Replace("'", "&apos;").Replace("&", "&amp;").Replace("<", "&lt;").Replace(">", "&gt;").Replace(ControlChars.Quote, "&quot;") & """ " & _
                                "/>")
                                sbXML.Append(vbNewLine)

                            Case "state"
                                sbXML.Append("<Row StateCode=""" & ldtProjects.Rows(i)(0).ToString().Replace("'", "&apos;").Replace("&", "&amp;").Replace("<", "&lt;").Replace(">", "&gt;").Replace(ControlChars.Quote, "&quot;") & """ " & _
                                 "State=""" & ldtProjects.Rows(i)(1).ToString().Replace("'", "&apos;").Replace("&", "&amp;").Replace("<", "&lt;").Replace(">", "&gt;").Replace(ControlChars.Quote, "&quot;") & """ " & _
                                "/>")
                                sbXML.Append(vbNewLine)

                            Case "style"
                                sbXML.Append("<Row Style=""" & ldtProjects.Rows(i)(0).ToString().Replace("'", "&apos;").Replace("&", "&amp;").Replace("<", "&lt;").Replace(">", "&gt;").Replace(ControlChars.Quote, "&quot;") & """ " & _
                                "/>")
                                sbXML.Append(vbNewLine)

                            Case "territory"
                                sbXML.Append("<Row Territory=""" & ldtProjects.Rows(i)(0).ToString().Replace("'", "&apos;").Replace("&", "&amp;").Replace("<", "&lt;").Replace(">", "&gt;").Replace(ControlChars.Quote, "&quot;") & """ " & _
                                "/>")
                                sbXML.Append(vbNewLine)

                            Case "ultimate parent"
                                sbXML.Append("<Row UltimateParent=""" & ldtProjects.Rows(i)(0).ToString().Replace("'", "&apos;").Replace("&", "&amp;").Replace("<", "&lt;").Replace(">", "&gt;").Replace(ControlChars.Quote, "&quot;") & """ " & _
                                "/>")
                                sbXML.Append(vbNewLine)

                            Case "turnover"
                                sbXML.Append("<Row Turnover=""" & ldtProjects.Rows(i)(0).ToString().Replace("'", "&apos;").Replace("&", "&amp;").Replace("<", "&lt;").Replace(">", "&gt;").Replace(ControlChars.Quote, "&quot;") & """ " & _
                                "/>")
                                sbXML.Append(vbNewLine)

                            Case "master investor type"
                                sbXML.Append("<Row MInvestorType=""" & ldtProjects.Rows(i)(0).ToString().Replace("'", "&apos;").Replace("&", "&amp;").Replace("<", "&lt;").Replace(">", "&gt;").Replace(ControlChars.Quote, "&quot;") & """ " & _
                                 "MInvestorTypeCode=""" & ldtProjects.Rows(i)(1).ToString().Replace("'", "&apos;").Replace("&", "&amp;").Replace("<", "&lt;").Replace(">", "&gt;").Replace(ControlChars.Quote, "&quot;") & """ " & _
                                "/>")
                                sbXML.Append(vbNewLine)

                            Case "investor type linking"
                                sbXML.Append("<Row InvestorTypeID=""" & ldtProjects.Rows(i)(0).ToString().Replace("'", "&apos;").Replace("&", "&amp;").Replace("<", "&lt;").Replace(">", "&gt;").Replace(ControlChars.Quote, "&quot;") & """ " & _
                                 "MInvestorTypeID=""" & ldtProjects.Rows(i)(1).ToString().Replace("'", "&apos;").Replace("&", "&amp;").Replace("<", "&lt;").Replace(">", "&gt;").Replace(ControlChars.Quote, "&quot;") & """ " & _
                                "/>")
                                sbXML.Append(vbNewLine)
                        End Select
                    End If
                Next
                sbXML.Append("</Root>")

            End If

        Catch ex As Exception
            MessageBox.Show(ex.Message)
            Logmessage.Append("There is error in grdClientDetail_AfterCellListCloseUp of form findmanagement.")
            Logmessage.Append(Environment.NewLine)
            Logmessage.Append("Error Message - ")
            Logmessage.Append(ex.ToString())
            Me.LogErrorToDatabase(Logmessage.ToString)
        End Try
        Return sbXML
    End Function

    Protected Function CreateUpdatedRecordXML(ByVal ldtProjects As DataTable) As StringBuilder
        Dim i As Integer
        Dim Logmessage As New System.Text.StringBuilder
        Dim sbXML As New StringBuilder
        sbXML.Append("<?xml version=" & """1.0" & """ encoding=" & """Windows-1252" & """?>")
        Try
            If ldtProjects.Rows.Count > 0 Then
                ' ldtProjects = (CType(grdClientDetail.DataSource, DataTable))
                sbXML.Append("<Root>")
                For i = 0 To ldtProjects.Rows.Count - 1
                    If Not CommonCLL.blnRowBlank(ldtProjects.Rows(i)) Then
                        Select Case combo.Trim.ToLower
                            Case "activist"
                                sbXML.Append("<Row ActivistID=""" & ldtProjects.Rows(i)(0).ToString().Replace("'", "&apos;").Replace("&", "&amp;").Replace("<", "&lt;").Replace(">", "&gt;").Replace(ControlChars.Quote, "&quot;") & """ " & _
                                 "ActivistCategory=""" & ldtProjects.Rows(i)(1).ToString().Replace("'", "&apos;").Replace("&", "&amp;").Replace("<", "&lt;").Replace(">", "&gt;").Replace(ControlChars.Quote, "&quot;") & """ " & _
                                "ActivistCode=""" & ldtProjects.Rows(i)(2).ToString().Replace("'", "&apos;").Replace("&", "&amp;").Replace("<", "&lt;").Replace(">", "&gt;").Replace(ControlChars.Quote, "&quot;") & """ " & _
                             "/>")
                                sbXML.Append(vbNewLine)

                            Case "city"
                                sbXML.Append("<Row CityID=""" & ldtProjects.Rows(i)(0).ToString().Replace("'", "&apos;").Replace("&", "&amp;").Replace("<", "&lt;").Replace(">", "&gt;").Replace(ControlChars.Quote, "&quot;") & """ " & _
                                 "City=""" & ldtProjects.Rows(i)(1).ToString().Replace("'", "&apos;").Replace("&", "&amp;").Replace("<", "&lt;").Replace(">", "&gt;").Replace(ControlChars.Quote, "&quot;") & """ " & _
                                 "CountryID=""" & ldtProjects.Rows(i)(2).ToString().Replace("'", "&apos;").Replace("&", "&amp;").Replace("<", "&lt;").Replace(">", "&gt;").Replace(ControlChars.Quote, "&quot;") & """ " & _
                             "/>")
                                sbXML.Append(vbNewLine)

                            Case "country"
                                sbXML.Append("<Row CountryID=""" & ldtProjects.Rows(i)(0).ToString().Replace("'", "&apos;").Replace("&", "&amp;").Replace("<", "&lt;").Replace(">", "&gt;").Replace(ControlChars.Quote, "&quot;") & """ " & _
                                 "CountryCode=""" & ldtProjects.Rows(i)(1).ToString().Replace("'", "&apos;").Replace("&", "&amp;").Replace("<", "&lt;").Replace(">", "&gt;").Replace(ControlChars.Quote, "&quot;") & """ " & _
                                "CountryName=""" & ldtProjects.Rows(i)(2).ToString().Replace("'", "&apos;").Replace("&", "&amp;").Replace("<", "&lt;").Replace(">", "&gt;").Replace(ControlChars.Quote, "&quot;") & """ " & _
                                 "RegionID=""" & ldtProjects.Rows(i)(3).ToString().Replace("'", "&apos;").Replace("&", "&amp;").Replace("<", "&lt;").Replace(">", "&gt;").Replace(ControlChars.Quote, "&quot;") & """ " & _
                             "/>")
                                sbXML.Append(vbNewLine)

                            Case "exchange"
                                sbXML.Append("<Row ExchangeID=""" & ldtProjects.Rows(i)(0).ToString().Replace("'", "&apos;").Replace("&", "&amp;").Replace("<", "&lt;").Replace(">", "&gt;").Replace(ControlChars.Quote, "&quot;") & """ " & _
                                 "Exchange=""" & ldtProjects.Rows(i)(1).ToString().Replace("'", "&apos;").Replace("&", "&amp;").Replace("<", "&lt;").Replace(">", "&gt;").Replace(ControlChars.Quote, "&quot;") & """ " & _
                               "/>")
                                sbXML.Append(vbNewLine)

                            Case "fund type"
                                sbXML.Append("<Row FundTypeID=""" & ldtProjects.Rows(i)(0).ToString().Replace("'", "&apos;").Replace("&", "&amp;").Replace("<", "&lt;").Replace(">", "&gt;").Replace(ControlChars.Quote, "&quot;") & """ " & _
                                 "FundType=""" & ldtProjects.Rows(i)(1).ToString().Replace("'", "&apos;").Replace("&", "&amp;").Replace("<", "&lt;").Replace(">", "&gt;").Replace(ControlChars.Quote, "&quot;") & """ " & _
                                "FundTypeDescription=""" & ldtProjects.Rows(i)(2).ToString().Replace("'", "&apos;").Replace("&", "&amp;").Replace("<", "&lt;").Replace(">", "&gt;").Replace(ControlChars.Quote, "&quot;") & """ " & _
                                "/>")
                                sbXML.Append(vbNewLine)

                            Case "head investor"
                                sbXML.Append("<Row HeadInvestorID=""" & ldtProjects.Rows(i)(0).ToString().Replace("'", "&apos;").Replace("&", "&amp;").Replace("<", "&lt;").Replace(">", "&gt;").Replace(ControlChars.Quote, "&quot;") & """ " & _
                                 "HeadInvestor=""" & ldtProjects.Rows(i)(1).ToString().Replace("'", "&apos;").Replace("&", "&amp;").Replace("<", "&lt;").Replace(">", "&gt;").Replace(ControlChars.Quote, "&quot;") & """ " & _
                                "/>")
                                sbXML.Append(vbNewLine)

                            Case "industry"
                                sbXML.Append("<Row IndustryID=""" & ldtProjects.Rows(i)(0).ToString().Replace("'", "&apos;").Replace("&", "&amp;").Replace("<", "&lt;").Replace(">", "&gt;").Replace(ControlChars.Quote, "&quot;") & """ " & _
                                 "Industry=""" & ldtProjects.Rows(i)(1).ToString().Replace("'", "&apos;").Replace("&", "&amp;").Replace("<", "&lt;").Replace(">", "&gt;").Replace(ControlChars.Quote, "&quot;") & """ " & _
                                "/>")
                                sbXML.Append(vbNewLine)

                            Case "investor type"
                                sbXML.Append("<Row InvestorTypeID=""" & ldtProjects.Rows(i)(0).ToString().Replace("'", "&apos;").Replace("&", "&amp;").Replace("<", "&lt;").Replace(">", "&gt;").Replace(ControlChars.Quote, "&quot;") & """ " & _
                                 "InvestorTypeCode=""" & ldtProjects.Rows(i)(1).ToString().Replace("'", "&apos;").Replace("&", "&amp;").Replace("<", "&lt;").Replace(">", "&gt;").Replace(ControlChars.Quote, "&quot;") & """ " & _
                             "IsExcludedFromSearch=""" & ldtProjects.Rows(i)(2).ToString().Replace("'", "&apos;").Replace("&", "&amp;").Replace("<", "&lt;").Replace(">", "&gt;").Replace(ControlChars.Quote, "&quot;") & """ " & _
                                "/>")
                                sbXML.Append(vbNewLine)

                            Case "metro"
                                sbXML.Append("<Row MetroID=""" & ldtProjects.Rows(i)(0).ToString().Replace("'", "&apos;").Replace("&", "&amp;").Replace("<", "&lt;").Replace(">", "&gt;").Replace(ControlChars.Quote, "&quot;") & """ " & _
                                 "Metro=""" & ldtProjects.Rows(i)(1).ToString().Replace("'", "&apos;").Replace("&", "&amp;").Replace("<", "&lt;").Replace(">", "&gt;").Replace(ControlChars.Quote, "&quot;") & """ " & _
                                "/>")
                                sbXML.Append(vbNewLine)

                            Case "region"
                                sbXML.Append("<Row RegionID=""" & ldtProjects.Rows(i)(0).ToString().Replace("'", "&apos;").Replace("&", "&amp;").Replace("<", "&lt;").Replace(">", "&gt;").Replace(ControlChars.Quote, "&quot;") & """ " & _
                                 "RegionCode=""" & ldtProjects.Rows(i)(1).ToString().Replace("'", "&apos;").Replace("&", "&amp;").Replace("<", "&lt;").Replace(">", "&gt;").Replace(ControlChars.Quote, "&quot;") & """ " & _
                                 "Region=""" & ldtProjects.Rows(i)(2).ToString().Replace("'", "&apos;").Replace("&", "&amp;").Replace("<", "&lt;").Replace(">", "&gt;").Replace(ControlChars.Quote, "&quot;") & """ " & _
                                "/>")
                                sbXML.Append(vbNewLine)

                            Case "sector"
                                sbXML.Append("<Row SectorID=""" & ldtProjects.Rows(i)(0).ToString().Replace("'", "&apos;").Replace("&", "&amp;").Replace("<", "&lt;").Replace(">", "&gt;").Replace(ControlChars.Quote, "&quot;") & """ " & _
                                 "Sector=""" & ldtProjects.Rows(i)(1).ToString().Replace("'", "&apos;").Replace("&", "&amp;").Replace("<", "&lt;").Replace(">", "&gt;").Replace(ControlChars.Quote, "&quot;") & """ " & _
                                "/>")
                                sbXML.Append(vbNewLine)

                            Case "state"
                                sbXML.Append("<Row StateID=""" & ldtProjects.Rows(i)(0).ToString().Replace("'", "&apos;").Replace("&", "&amp;").Replace("<", "&lt;").Replace(">", "&gt;").Replace(ControlChars.Quote, "&quot;") & """ " & _
                                 "StateCode=""" & ldtProjects.Rows(i)(1).ToString().Replace("'", "&apos;").Replace("&", "&amp;").Replace("<", "&lt;").Replace(">", "&gt;").Replace(ControlChars.Quote, "&quot;") & """ " & _
                                 "State=""" & ldtProjects.Rows(i)(2).ToString().Replace("'", "&apos;").Replace("&", "&amp;").Replace("<", "&lt;").Replace(">", "&gt;").Replace(ControlChars.Quote, "&quot;") & """ " & _
                                 "CountryID=""" & ldtProjects.Rows(i)(3).ToString().Replace("'", "&apos;").Replace("&", "&amp;").Replace("<", "&lt;").Replace(">", "&gt;").Replace(ControlChars.Quote, "&quot;") & """ " & _
                                "/>")
                                sbXML.Append(vbNewLine)

                            Case "style"
                                sbXML.Append("<Row StyleID=""" & ldtProjects.Rows(i)(0).ToString().Replace("'", "&apos;").Replace("&", "&amp;").Replace("<", "&lt;").Replace(">", "&gt;").Replace(ControlChars.Quote, "&quot;") & """ " & _
                                 "Style=""" & ldtProjects.Rows(i)(1).ToString().Replace("'", "&apos;").Replace("&", "&amp;").Replace("<", "&lt;").Replace(">", "&gt;").Replace(ControlChars.Quote, "&quot;") & """ " & _
                                "/>")
                                sbXML.Append(vbNewLine)

                            Case "territory"
                                sbXML.Append("<Row TerritoryID=""" & ldtProjects.Rows(i)(0).ToString().Replace("'", "&apos;").Replace("&", "&amp;").Replace("<", "&lt;").Replace(">", "&gt;").Replace(ControlChars.Quote, "&quot;") & """ " & _
                                 "Territory=""" & ldtProjects.Rows(i)(1).ToString().Replace("'", "&apos;").Replace("&", "&amp;").Replace("<", "&lt;").Replace(">", "&gt;").Replace(ControlChars.Quote, "&quot;") & """ " & _
                                "/>")
                                sbXML.Append(vbNewLine)

                            Case "ultimate parent"
                                sbXML.Append("<Row UltimateParentID=""" & ldtProjects.Rows(i)(0).ToString().Replace("'", "&apos;").Replace("&", "&amp;").Replace("<", "&lt;").Replace(">", "&gt;").Replace(ControlChars.Quote, "&quot;") & """ " & _
                                 "UltimateParent=""" & ldtProjects.Rows(i)(1).ToString().Replace("'", "&apos;").Replace("&", "&amp;").Replace("<", "&lt;").Replace(">", "&gt;").Replace(ControlChars.Quote, "&quot;") & """ " & _
                                "/>")
                                sbXML.Append(vbNewLine)

                            Case "master investor type"
                                sbXML.Append("<Row MInvestorTypeID=""" & ldtProjects.Rows(i)(0).ToString().Replace("'", "&apos;").Replace("&", "&amp;").Replace("<", "&lt;").Replace(">", "&gt;").Replace(ControlChars.Quote, "&quot;") & """ " & _
                                 "MInvestorType=""" & ldtProjects.Rows(i)(1).ToString().Replace("'", "&apos;").Replace("&", "&amp;").Replace("<", "&lt;").Replace(">", "&gt;").Replace(ControlChars.Quote, "&quot;") & """ " & _
                                 "MInvestorTypeCode=""" & ldtProjects.Rows(i)(2).ToString().Replace("'", "&apos;").Replace("&", "&amp;").Replace("<", "&lt;").Replace(">", "&gt;").Replace(ControlChars.Quote, "&quot;") & """ " & _
                                "/>")
                                sbXML.Append(vbNewLine)

                            Case "investor type linking"
                                sbXML.Append("<Row TypeMTypeLinkID=""" & ldtProjects.Rows(i)(0).ToString().Replace("'", "&apos;").Replace("&", "&amp;").Replace("<", "&lt;").Replace(">", "&gt;").Replace(ControlChars.Quote, "&quot;") & """ " & _
                                 "InvestorTypeID=""" & ldtProjects.Rows(i)(1).ToString().Replace("'", "&apos;").Replace("&", "&amp;").Replace("<", "&lt;").Replace(">", "&gt;").Replace(ControlChars.Quote, "&quot;") & """ " & _
                                 "MInvestorTypeID=""" & ldtProjects.Rows(i)(2).ToString().Replace("'", "&apos;").Replace("&", "&amp;").Replace("<", "&lt;").Replace(">", "&gt;").Replace(ControlChars.Quote, "&quot;") & """ " & _
                                "/>")
                                sbXML.Append(vbNewLine)
                        End Select
                    End If
                Next
                sbXML.Append("</Root>")

            End If

        Catch ex As Exception
            MessageBox.Show(ex.Message)
            Logmessage.Append("There is error in grdClientDetail_AfterCellListCloseUp of form findmanagement.")
            Logmessage.Append(Environment.NewLine)
            Logmessage.Append("Error Message - ")
            Logmessage.Append(ex.ToString())
            Me.LogErrorToDatabase(Logmessage.ToString)
        End Try
        Return sbXML
    End Function

    Private Sub MessageInfo(ByVal message As String)
        MessageBox.Show(message, BaseClass.crystalCaption, MessageBoxButtons.OK, MessageBoxIcon.Information)
    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Dim dataString As String = String.Empty
        Dim obj As New cAddEditLKPTable
        Dim dataExists As String = String.Empty
        Dim dataSet As DataSet = Nothing
        Dim dataGrid As New DataTable
        Try
            CheckSaved = False
            If Validation(msg) Then
                If grdRecords.ActiveRow.Cells(0).Value.ToString <> String.Empty Then
                    If (DonotUpdateIfChangesNotMade() = 0) Then
                        dataExists = RecordAlreadyExist()
                        If dataExists = String.Empty Then
                            GetUpdatedRows()
                        End If
                    End If
                End If
                If dataTable Is Nothing Then
                    FillDataTable()
                    AddDataToDataTable()
                End If
            Else
                MessageBox.Show(msg, BaseClass.crystalCaption, MessageBoxButtons.OK, MessageBoxIcon.Information)
                Exit Sub
            End If
            If updatedDataTable IsNot Nothing Then
                If updatedDataTable.Rows.Count > 0 Then
                    updatedDataTable.Columns.Remove("Delete")
                    CheckAddUpdateRecord(updatedDataTable, False)
                    dataString = CreateUpdatedRecordXML(updatedDataTable).ToString
                    dataSet = obj.UpdateRecords(cmbMasterTable.SelectedValue, dataString)
                    updatedDataTable = Nothing
                    If dataSet IsNot Nothing Then
                        If dataTable IsNot Nothing Then
                            If dataTable.Rows.Count = 0 Then
                                MessageBox.Show("Record updated successfully.", BaseClass.crystalCaption, MessageBoxButtons.OK, MessageBoxIcon.Information)
                                CheckSaved = True
                            End If
                        End If
                    End If
                End If
            End If
            If dataTable Is Nothing Then Exit Sub
            If dataTable.Rows.Count = 0 Then Exit Sub
            Dim msgs As String = String.Empty
            For Each row As DataRow In dataTable.Rows
                ValidateRow(row, msgs)
                If msgs <> String.Empty Then
                    MessageBox.Show(msgs, BaseClass.crystalCaption, MessageBoxButtons.OK, MessageBoxIcon.Information)
                    Exit Sub
                End If
            Next
            CheckAddUpdateRecord(dataTable, True)
            CheckSameRecord(dataTable)
            If dataTable.Rows.Count = 0 Then
                If CheckSaved = False Then
                    MessageBox.Show("Record already exists.", BaseClass.crystalCaption, MessageBoxButtons.OK, MessageBoxIcon.Information)
                End If
                Exit Sub
            End If
            dataString = CreateListingXML(dataTable).ToString
            dataSet = obj.AddNewRecords(cmbMasterTable.SelectedValue, dataString)
            If dataSet IsNot Nothing Then
                MessageBox.Show("Record saved successfully.", BaseClass.crystalCaption, MessageBoxButtons.OK, MessageBoxIcon.Information)
                CheckSaved = True
            End If
            If ValidatedData IsNot Nothing Then
                saveAndClose = True
                btnExit.PerformClick()
            Else
                If dataSet IsNot Nothing Then
                    If dataSet.Tables IsNot Nothing Then
                        BindGridData()
                    End If
                End If
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message, BaseClass.crystalCaption, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            obj = Nothing
            dataExists = String.Empty
            dataString = String.Empty
            dataTable = Nothing
            updatedDataTable = Nothing

        End Try
    End Sub
End Class